var A = function () {this.objectsOwnProperties = "are serialized";};A.prototype.prototypeProperties = "are NOT serialized";var instance = new A();console.log(instance.prototypeProperties); // "are NOT serialized"console.log(JSON.stringify(instance));// {"objectsOwnProperties":"are serialized"}
// x is a method assigned to the object using "this"var A = function () {this.x = function () { alert('A'); };};A.prototype.updateX = function( value ) {this.x = function() { alert( value ); }};
var a1 = new A();var a2 = new A();a1.x(); // Displays 'A'a2.x(); // Also displays 'A'a1.updateX('Z');a1.x(); // Displays 'Z'a2.x(); // Still displays 'A'
// Here x is a method assigned to the object using "prototype"var B = function () { };B.prototype.x = function () { alert('B'); };
B.prototype.updateX = function( value ) {B.prototype.x = function() { alert( value ); }}
var b1 = new B();var b2 = new B();b1.x(); // Displays 'B'b2.x(); // Also displays 'B'b1.updateX('Y');b1.x(); // Displays 'Y'b2.x(); // Also displays 'Y' because by using prototype we have changed it for all instances
var A = function (param1) {var privateVar = null; // Private variable
// Calling this.setPrivateVar(param1) here would be an error
this.setPrivateVar = function (value) {privateVar = value;console.log("setPrivateVar value set to: " + value);
// param1 is still here, possible memory leakconsole.log("setPrivateVar has param1: " + param1);};
// The constructor logic starts here possibly after// many lines of code that define methods
this.setPrivateVar(param1); // This is valid};
var a = new A(0);// setPrivateVar value set to: 0// setPrivateVar has param1: 0
a.setPrivateVar(1);//setPrivateVar value set to: 1//setPrivateVar has param1: 0
对:
var A = function (param1) {this.setPublicVar(param1); // This is valid};A.prototype.setPublicVar = function (value) {this.publicVar = value; // No private variable};
var a = new A(0);a.setPublicVar(1);console.log(a.publicVar); // 1
var AdultPerson = function() {
var age;
this.setAge = function(val) {// some housekeepingage = val >= 18 && val;};
this.getAge = function() {return age;};
this.isValid = function() {return !!age;};};
现在,prototype结构可以应用如下:
不同的成年人有不同的年龄,但所有的成年人都有相同的权利。 所以,我们使用原型添加它,而不是这个。
AdultPerson.prototype.getRights = function() {// Should be validreturn this.isValid() && ['Booze', 'Drive'];};
现在让我们看看执行情况。
var p1 = new AdultPerson;p1.setAge(12); // ( age = false )console.log(p1.getRights()); // false ( Kid alert! )p1.setAge(19); // ( age = 19 )console.log(p1.getRights()); // ['Booze', 'Drive'] ( Welcome AdultPerson )
var p2 = new AdultPerson;p2.setAge(45);console.log(p2.getRights()); // The same getRights() method, *** not a new copy of it ***
var Car = function(loc) {this.loc = loc;};
Car.prototype.move = function() {this.loc++;};
var amy = new Car(1);amy.move();var ben = new Car(9);ben.move();
var A = function () {this.x = function () {//do something};};
var a = new A(); // constructor function gets executed// newly created object gets an 'x' property// which is a functiona.x(); // and can be called like this
在第二个示例中,您将向原型对象添加一个属性,所有使用A创建的实例都指向该属性。
var A = function () { };A.prototype.x = function () {//do something};
var a = new A(); // constructor function gets executed// which does nothing in this example
a.x(); // you are trying to access the 'x' property of an instance of 'A'// which does not exist// so JavaScript looks for that property in the prototype object// that was defined using the 'prototype' property of the constructor