The Closure Library implements classes with properties defined both on the prototype and inside the constructor function. In addition to exploring the source code for the Closure library, we’ll look at some questions when deciding whether to define a property in a prototype or constructor.
?
, (, VIN ), , .
Car = function(vin) {
this.vin_ = vin;
};
(, , , ) (, Object, Array)?
, . , , .
Car.prototype.cylinders_ = 4;
Car.prototype.setCylinders = function(cylinders) {
if (this.cylinders_ == cylinders) {
return;
}
this.cylinders_ = cylinders;
};
Car.prototype.getCylinders = function() {
return this.cylinders_;
};
, .
var myCar = new Car("1HGCM82633A004352");
alert(myCar.getCylinders());
myCar.setCylinders(6);
alert(myCar.getCylinders());
delete myCar.cylinders_;
alert(myCar.getCylinders());
, Array Object, mutable . , , .
null?
Closure , null.. , , , 4, .