Examples of JavaScript inheritance mode. For more information, see.
The Code is as follows:
Function parent (){
This. x = 10;
}
Function child (){
Var parentObj = new parent ();
For (var p in parentObj) this [p] = parentObj [p];
}
Var childObj = new child ();
Alert (childObj. x );
The Code is as follows:
Function parent (){
This. x = 10;
}
Function child (){
This. parent = parent;
This. parent ();
Delete this. parent;
}
Var childObj = new child ();
Alert (childObj. x );
The Code is as follows:
Function parent (){
This. x = 10;
}
Function child (){
Parent. call (this );
}
Var childObj = new child ();
Alert (childObj. x );
Prototype transcription
The Code is as follows:
Function parent (){
}
Parent. prototype. x = 1;
Function child (){
}
For (var p in parent. prototype) child. prototype [p] = parent. prototype [p];
Child. prototype. y = 2;
Var childObj = new child ();
Alert (childObj. x );
The Code is as follows:
Function parent (string ){
Var child = new Function ("this. x = 10;" + string );
Return child;
}
Var child = new parent ("this. y = 20 ;");
Var childObj = new child ();
Alert (childObj. y );
The Code is as follows:
Function parent (){
This. x = 10;
}
Function child (){
}
Child. prototype = new parent ();
Var childObj = new child ();
Alert (childObj. x );
The Code is as follows:
Function parent (){
This. x = 10;
}
Function child (){
Var ret = new parent ();
Ret. y = 20;
Return ret;
}
Var childObj = new child ();
Alert (childObj. x );