Can JavaScript classes/objects have constructors?(JavaScript类/对象可以具有构造函数吗?)
Using prototypes:(使用原型:)
function Box(color) // Constructor { this.color = color; } Box.prototype.getColor = function() { return this.color; };
function Box(col) { var color = col; this.getColor = function() { return color; }; }
var blueBox = new Box("blue"); alert(blueBox.getColor()); // will alert blue var greenBox = new Box("green"); alert(greenBox.getColor()); // will alert green
1.4m articles
1.4m replys
5 comments
57.0k users