4. Constructor + Prototype Directly assemble a class; the same constructor will assemble the same type
Through the previous articles, I learned that JavaScript writing classes are nothing more than constructors and prototypes. In this case, we write a tool function to write the class.
/**
* $class write tool function
* @param {Object} constructor
* @param {Object} prototype
*/
function $class(constructor,prototype) {
var c = constructor || function(){};
var p = prototype || {};
= p;
return c;
}
Um. The tool class has been written, and assembled: Use the constructor to generate the attributes (fields) of the class instance, and the method used by the prototype object to generate the class instance.
//Constructor
function Person(name) {
= name;
}
//Prototype object
var proto = {
getName : function(){return },
setName : function(name){ = name;}
}
//Assembly
var Man = $class(Person,proto);
var Woman = $class(Person,proto);
OK, at this time, I have already obtained two types of Man, Woman. And it is of the same type. The tests are as follows:
(Man == Woman);//true
( == );//true
Create an object and see it.
var man = new Man("Andy");
var woman = new Woman("Lily");
(man instanceof Man);//true
(woman instanceof Woman);//true
(man instanceof Person);//true
(woman instanceof Person);//true
OK Everything is as we expected. But there is a problem, the result of the following code outputs false.
( == Person);//false
This is unpleasant: from the above code, it can be seen that man is indeed created through the Man class new var man = new Man("Andy"), so the constructor of the object instance man should point to Man, but why is it contrary to expectations?
The reason is that Person's prototype was rewritten in $class: = p;
OK, let's rewrite $class slightly and hang all the methods on the constructor's prototype (rather than rewrite the constructor's prototype), as follows:
function $class(constructor,prototype) {
var c = constructor || function(){};
var p = prototype || {};
// = p;
for(var atr in p)
[atr] = p[atr];
return c;
}
Through the previous articles, I learned that JavaScript writing classes are nothing more than constructors and prototypes. In this case, we write a tool function to write the class.
Copy the codeThe code is as follows:
/**
* $class write tool function
* @param {Object} constructor
* @param {Object} prototype
*/
function $class(constructor,prototype) {
var c = constructor || function(){};
var p = prototype || {};
= p;
return c;
}
Um. The tool class has been written, and assembled: Use the constructor to generate the attributes (fields) of the class instance, and the method used by the prototype object to generate the class instance.
Copy the codeThe code is as follows:
//Constructor
function Person(name) {
= name;
}
//Prototype object
var proto = {
getName : function(){return },
setName : function(name){ = name;}
}
//Assembly
var Man = $class(Person,proto);
var Woman = $class(Person,proto);
OK, at this time, I have already obtained two types of Man, Woman. And it is of the same type. The tests are as follows:
Copy the codeThe code is as follows:
(Man == Woman);//true
( == );//true
Create an object and see it.
Copy the codeThe code is as follows:
var man = new Man("Andy");
var woman = new Woman("Lily");
(man instanceof Man);//true
(woman instanceof Woman);//true
(man instanceof Person);//true
(woman instanceof Person);//true
OK Everything is as we expected. But there is a problem, the result of the following code outputs false.
Copy the codeThe code is as follows:
( == Person);//false
This is unpleasant: from the above code, it can be seen that man is indeed created through the Man class new var man = new Man("Andy"), so the constructor of the object instance man should point to Man, but why is it contrary to expectations?
The reason is that Person's prototype was rewritten in $class: = p;
OK, let's rewrite $class slightly and hang all the methods on the constructor's prototype (rather than rewrite the constructor's prototype), as follows:
Copy the codeThe code is as follows:
function $class(constructor,prototype) {
var c = constructor || function(){};
var p = prototype || {};
// = p;
for(var atr in p)
[atr] = p[atr];
return c;
}