6, the Prototype.js writing class way
Copy Code code as follows:
Code in the Prototype.js
var Class = {
Create:function () {
return function () {
This.initialize.apply (this, arguments);
}
}
}
After simplifying the
function Clazz () {
return function () {
This.initialize.apply (this,arguments);
}
}
The following steps write a class,
Copy Code code as follows:
Class name Person
var person = class.create ();
To define a person by a prototype rewrite
Person.prototype = {
Initialize:function (name) {
THIS.name = name;
},
Getname:function () {
return this.name;
},
Setname:function (name) {
THIS.name = name;
}
}
Creating objects
var p = new Person ("Jack");
Console.log (P.constructor = = person);//false
Initialize completes the initialization of the object (equivalent to the constructor), and the method is written down in turn.
There is a problem, through this p.constructor = = person as false can be seen, this is prototype.js a small flaw. The reason is to rewrite the person's prototype. In order for the constructor to point to the correct constructor, it is only necessary to maintain the constructor property when the prototype is rewritten.
Copy Code code as follows:
Person.prototype = {
constructor:person,//, look here.
Initialize:function (name) {
THIS.name = name;
},
Getname:function () {
return this.name;
},
Setname:function (name) {
THIS.name = name;
}
}
Well, this time P.constructor = = person is true.