Home > Web Front-end > JS Tutorial > The fifth way to write classes in javascript_js object-oriented

The fifth way to write classes in javascript_js object-oriented

WBOY
Release: 2016-05-16 18:50:46
Original
888 people have browsed it

5. Use the constructor prototype to define a class; the same constructor can define multiple types

Copy the code The code is as follows:

/**
* $define class writing tool function 2
* @param {Object} constructor
* @param {Object} prototype
*/
function $define(constructor,prototype) {
var c = constructor || function(){};
var p = prototype | | {};
return function() {
for(var atr in p)
arguments.callee.prototype[atr] = p[atr];
c.apply(this,arguments) ;
}
}

Similar to the fourth method, two classes are still defined using constructors and prototype objects.
Copy code The code is as follows:

//Constructor
function Person(name) {
this.name = name;
}
//Prototype object
var proto = {
getName : function(){return this.name},
setName : function( name){this.name = name;}
}
//Define two classes
var Man = $define(Person,proto);
var Woman = $define(Person,proto) ;
console.log(Man == Woman); //false, the same constructor (Person) defines different classes
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template