1. Prototype chain inheritance (the prototype object of the constructor equals the object instantiated by another constructor)
function Box() {
this.name=' Lee ';
}
function Desk() {
this.age= +;
}
Desk.prototype=new box ();//desk inherited box, forming a prototype chain;
var desk=new desk ();
alert (desk.name);//lee
function Table () {
this.height=100;
}
Table.prototype=new Desk ();//Table Inherits Desk
var table=new table ();//table has two inherited properties
Use the prototype chain to inherit the existing problem:
1: There is a problem with data modification when there is a reference type in the prototype
2: Object of subclass type cannot pass arguments to parent class
2. Object Impersonation
function Test(age) {
this.family=["Mom","Dad"];
this.age=age;
}
function Util(age) {
Test.call (this,age); //Indicates which object to call the test function
//this is the Util object we created.
}
Object posing problem: Cannot use prototype all cannot implement data sharing
3. Combination Inheritance (prototype chain and object impersonation)
function Test(age) {
this.family=["Mom","Dad"];
this.age=age;
}
Test.prototype. Fun=function () {
return this.age+this.family;
}
function Util(age) {
Test.call (this,age); //Object Impersonation
}
Util.prototype=new Text (); This is the prototype chain .
var u=new Util (+);
alert (u.age);
4. Prototype inheritance// O is an object
function getobj(o) {//Return prototype is an object of O
function F() {}
F.prototype=o;
return new F ();
}
Prototype Object
var person={
Name:"Tom",
Family:{' Mom ', ' Dad '}
}
var obj1=getobj (person);
5. Parasitic Inheritance (prototype + Factory mode)
function getobj(o) {
function F() {};
f.prototype=o;
Obj.arr=["html",' css ');
return new F ();
}
Parasitic functions
function createobj(o) {
var obj=getobj (o);
return obj;
}
Prototype Object
var person={
name:"Han",
Age:
}
var a=createobj (person);
Javascript-Object-Oriented inheritance