Home > Web Front-end > JS Tutorial > Implementation code of js inheritance_javascript skills

Implementation code of js inheritance_javascript skills

WBOY
Release: 2016-05-16 18:21:47
Original
1453 people have browsed it

base.js --implementation of inheritance ==========================
[Note]: After inheritance, if the parent class is a class , it will inherit its properties, methods (including those declared with prototype), and static methods, otherwise only properties and methods will be inherited.

Copy code The code is as follows:

Object.prototype.extendf= function (a,b) {
if(!a||!b) return;
var fa = typeof a=="function";
var fb = typeof b=="function";
var cha = function (a,b){
for(var c in b){
if(a[c]==undefined)//Subclass rewrite
a[c]=b[c];
}
return a; //Return the inherited object
}
if(fa&&fb){
b.apply(this,a.arguments);
cha(a,b );
this["base"] =new b;//Access the parent class through base
return cha(this,b.prototype);
}
else if(!fa&&fb){
cha(a,new b);
a["base"]= new b;
return cha(a,b);
}else if(fa&&!fb){
cha (a,b);
this["base"]=b;
return cha(this,b);
}else if(!fa&&!fb){
a["base" ]=b;
return cha(a,b);
}
}

Test page: Usage
Copy code The code is as follows:







js test:






< /div>



ps: I haven’t noticed any performance issues, and I’d like to ask everyone to improve it.
I want to use only one parameter, is there any way?
Nested class I haven’t tried it yet.
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