#1 default stereotype inheritance
function Inherint (c,p) {
C.prototype = new P ();
}
function Parent (name) {
THIS.name =name| | " Adam ";
}
Parent.prototype.say = function () {
return this.name;
}
function child (name) {
THIS.name = name;
}
Inherint (child,parent);
var kid = new Child ("Lishuang");
Kid.say (); //"Adam"
This---
Child {name: "Adam", say:function}
Disadvantages:
1 inherits the attributes on the two objects that are added to this and added to the prototype, most of which do not allow these self-properties
2 an inheritance function is called every time a subclass is required
3 Reference properties on the prototype are shared
#2 borrowing Constructors
Borrowing constructors
function article () {
This.tags =[' js ', ' CSS ';
}
var article = new article ();
function blogpost () {}
Blogpost.prototype = article;
var blog = new blogpost ();
Note that the above does not require new article () because there are already available instances
function Staticpage () {
Article.call (this);
}
var page = new Staticpage ();
Console.log (Article.hasownproperty (' tags '));
Console.log (Blog.hasownproperty (' tags ')); // get a reference on a prototype
Console.log (Page.hasownproperty (' tags ')); // creates a copy instead of a reference
True
False
True
Disadvantages:
Nothing can be inherited from the prototype, and the prototype is simply a place to add reusable methods and properties that will not recreate the prototype for each strength
Advantages
You can get the true copy of the parent object's own member without the risk that child objects will accidentally overwrite the parent object's properties
#3 borrowing and setting up prototypes
function Parent (name) {
THIS.name =name| | " Adam ";
}
Parent.prototype.say = function () {
return this.name;
}
function child (name) {
Parent.apply (this,arguments);
}
Child.prototype = new Parent ();
var kid = new Child ("Lishuang");
Kid.name;
"Lishuang"
Kid.say ()
"Lishuang"
Delete Kid.name
True
Kid.say ()
"Adam"
#4 shared prototype chain ------ descendant objects modifying prototypes alters the prototype of the parent object
function Inherint (c,p) {
C.prototype=p.prototype;
}
#5 Temporary Constructors
function inherit (c,p) {
var F = function () {};
F.prototype = P.prototype;
C.prototype = new F ();
}
Storage Super Class
function inherit (c,p) {
var F = function () {};
F.prototype = P.prototype;
C.prototype = new F ();
C.uber = P.prototype;
}
Resetting constructors
var inherit = (function () {
var F = function () {};
return function (c,p) {
F.prototype = P.prototype;
C.prototype = new F ();
C.uber = P.prototype;
C.prototype.constructor = C;
}
}())
5 prototype Inheritance
function Object (o) {
Functionf () {};
F.prototype= o;
Returnnew F ();
}
function person () {
This.name= "Adam";
}
Person.prototype.getname=function () {
Returnthis.name;
}
var para = Newperson ();
var kid = object (para); // improved var kid = object (Person.prototype);
Kid.getname ();
ECMAscript5 is implemented with Object.create () .
The second argument of Object (parent,{}) is optional
YUI3 Y.object () method
YUI (). Use (' * ', function (Y) {
var child = Y.object (parent);
})
6// replication for inheritance
Shallow copy the risk of modifying the parent element when the property is an array of objects that is passed through drinking
Functionextend (parent,child) {
Vari
child= Child | | {};
for (iin parent) {
Child[i]= Parent[i];
}
Returnchild;
}
Deeply assigned value
Functionextenddeep (parent,child) {
var i,
Tostr=object.prototype.tostring,
ASTR = "[Object Array]";
child=child| | {};
For (i in parent) {
if (Parent.hasownproperty (i)) {
if (typeofparent[i] = = = "Object") {
Child[i]= (Tostr.call (parent[i)) ===astr? []:{};
Extenddeep (Parent[i],child[i]);
}else{
Child[i]= Parent[i];
}
}
}
return child;
}
function binding resolves, callback functions and the This of function expressions point to Global object issues
var one = {
Name: "Obj",
Say:function (greet) {return greet+ "," + this.name;}
}
One.say (' Hi ');
"Hi,obj"
Varsay = One.say; Say (' Hi ');
"Hi,"
function bind (o,m) {
return function () {
Returnm.apply (O,[].slice.call (arguments));// closures are still accessible after the function is returned o/m
};
}
The realization of Function.prototype.bind in ES5
if (typeoffunction.prototype.bind=== "undefined") {
function.prototype.bind= function (thisarg) {
VARFN = this,
Slice = Array.prototype.slice,
arg= Slice.call (arguments,1);
return function () {
Returnfn.apply (Thisarg,arg.concat (Slice.call (arguments)));
}
}
}
JS Inheritance Mode