-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.js
More file actions
21 lines (21 loc) · 853 Bytes
/
class.js
File metadata and controls
21 lines (21 loc) · 853 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//可以看生成的js就知道了 name,width height是constructor私有的变量 area 和color 是私有属性
var Shape = (function () {
function Shape(name, width, height) {
this.name = name;
this.area = width * height;
this.color = "pink";
}
;
Shape.prototype.shoutout = function () {
return "I'm " + this.color + " " + this.name + " with an area of " + this.area + " cm squared.";
};
return Shape;
}());
//构造器中参数(name, width 和 height) 的作用域是局部变量
var square = new Shape("square", 30, 30);
console.log(square.shoutout());
console.log('Area of Shape: ' + square.area);
console.log('Name of Shape: ' + square.name);
console.log('Color of Shape: ' + square.color);
console.log('Width of Shape: ' + square.width);
console.log('Height of Shape: ' + square.height);