JavaScript has no integrated object, so we use the following three methods to simulate it:
1. js prototype implementation inheritance
Function Person (name, age ){
This. name = name;
This. age = age;
}
Person. prototype. sayHello = function (){
Document. write ("Use the prototype to get Name:" + this. name + "</br> ");
}
// Var per = new Person ("zhangping", "21 ");
// Per. sayHello ();
Function Student (){}
Student. prototype = new Person ("zhangping", "21 ");
Var stu = new Student ();
Student. prototype. gade = "3 ";
Student. prototype. intr = function (){
Document. write (this. gade );
}
Stu. sayHello ();
Stu. intr ();
*/
2. constructor implementation inheritance
/*
Function Parent (name ){
This. name = name;
This. sayParent = function (){
Document. write ("Parent:" + this. name );
}
}
Function Child (name, age ){
This. tempMethod = Parent;
This. tempMethod (name );
/* Www.2cto.com
This. age = age;
This. sayParent = function (){
Document. write ("Child:" + this. name + "age:" + this. age );
}
*/
/*
}
Var parent = new Parent ("zhangping ");
Parent. sayParent ();
Var child = new Child ("xiaoguanxianfei", "11 ");
Child. sayParent ();
*/
3. Use Call Applay to implement inheritance
Function Person (name, age, love ){
This. name = name;
This. age = age;
This. love = love;
This. say = function say (){
Document. write ("name:" + name );
}
}
Function student (name, age ){
Person. call (this, name, age );
}
Function teacher (name, love ){
Person. apply (this, [name, love]);
}
Var per = new Person ("zhangping", "21", "guanxianfei ");
Per. say ();
Var stu = new student ("guanxianfei", "22 ");
Stu. say ();
Var tea = new teacher ("xiaoguanxianfei", "22 ");
Tea. say ();
From guanxianfei