In the previous article, I wrote a tool function $class, and this article will improve it as follows. Implement the following functions
1, inheritance
2. When a subclass inherits a parent class, it does not inherit the private attributes of the parent class
/**
* @param {String} className
* @param {String/Function} superCls
* @param {Function} classImp
*/
function $class(className, superCls, classImp){
if(superCls === '') superCls = Object;
function clazz (){
= new superCls();
var _super = superCls.prototype;
window[className] = clazz;
classImp.apply(p, [_super]);
}
Write a parent class first
Copy the code
*/
$class('Person','',function(){
// Private property age
var age;
this.init = function(n, a){
return this.name;
};
this.setName = function(name){
this.name = name;
}
this.getAge = function(){
return age;
};
this.setAge = function(a){
age = a;
};
});
Write a subclass, Inherited from Person
Copy code
The code is as follows:
$class("Man",Person, function(supr){
};
this.setSchool = function(s){
school = s;
};
});
new a subclass instance
Copy code
The code is as follows :
var m = new Man('tom', 25, 'pku');
through the getSchool() method
Statement: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