Transferred from:Http://www.cnblogs.com/snandy/archive/2011/03/07/1973241.html
The tool functions are as follows:
02 |
* $ Class write tool function 3 |
03 |
* @param {String} className |
04 |
* @param {Function} superClass |
05 |
* @param {Function} classImp |
07 |
function $class(className, superClass, classImp){ |
08 |
if(superClass === "") superClass = Object; |
11 |
if(typeof this.init == "function"){ |
12 |
this.init.apply(this, arguments); |
16 |
var p = clazz.prototype = new superClass(); |
17 |
var _super = superClass.prototype; |
18 |
window[className] = clazz; |
19 |
classImp.apply(p, [_super]); |
Define a person class
01 |
$class('Person', '', function(){ |
03 |
this.init = function(name){ |
07 |
this.getName = function(){ |
10 |
this.setName = function(name){ |
Create an object
1 |
var p = new Person('Jack'); |
3 |
console.log(p.constructor == Person); // false |
Use this tool to write classes. This. init method is essential. Students who have used prototype Libraries know that the initialize method after class. Create is also essential.
Because inheritance is not considered, the second parameter superclass uses an empty string, that is, it inherits from the object by default.
If the output is false, this tool class does not maintain the constructor attribute. Each write type of the design has a trade-off, which is entirely dependent on the intent of the designer.
The tool functions are as follows:
02 |
* $ Class write tool function 3 |
03 |
* @param {String} className |
04 |
* @param {Function} superClass |
05 |
* @param {Function} classImp |
07 |
function $class(className, superClass, classImp){ |
08 |
if(superClass === "") superClass = Object; |
11 |
if(typeof this.init == "function"){ |
12 |
this.init.apply(this, arguments); |
16 |
var p = clazz.prototype = new superClass(); |
17 |
var _super = superClass.prototype; |
18 |
window[className] = clazz; |
19 |
classImp.apply(p, [_super]); |
Define a person class
01 |
$class('Person', '', function(){ |
03 |
this.init = function(name){ |
07 |
this.getName = function(){ |
10 |
this.setName = function(name){ |
Create an object
1 |
var p = new Person('Jack'); |
3 |
console.log(p.constructor == Person); // false |
Use this tool to write classes. This. init method is essential. Students who have used prototype Libraries know that the initialize method after class. Create is also essential.
Because inheritance is not considered, the second parameter superclass uses an empty string, that is, it inherits from the object by default.
If the output is false, this tool class does not maintain the constructor attribute. Each write type of the design has a trade-off, which is entirely dependent on the intent of the designer.