Explanation of js Object Inheritance

Source: Internet
Author: User

// Inheritance in javascript-first Inheritance Method: Object impersonating
Function Parent (name)
{
This. name = name;
This. getInfo = function ()
{
Document. write ("Father's name is:" + name + "<br/> ");
}
}
Function Child (name, age)
{
// This is the most important three-line code to inherit the parent class
This. mehod = Parent; // The Code assigns the Parent object to this. method, which inherits the attributes and methods of the Parent class.
This. mehod (name); // this code initializes an attribute of the parent class.
Delete this. method; // delete because menthod has no effect

This. age = age;
This. getChildInfo = function ()
{
Document. write ("Son name:" + this. name + ", age:" + this. age );
}
}
 
Var parent = new Parent ("dad ");
// Parent. getInfo ();
Var child = new Child ("son", 24 );
// Child. getInfo ();
// Child. getChildInfo ();

 

// The call method is a method of every Function. Therefore, every Function we define has this method. The first parameter of the call method must be passed to this, the following parameters are passed to the function at a time.
Function Test (name, age)
{
This. name = name;
This. age = age;

This. getInfo = function ()
{
Document. write ("name:" + this. name + ", age:" + this. age );
}
}
Var obj1 = new Object ();
Obj1.sex = "male ";
Obj1.get = function ()
{
Document. write ("name:" + this. name + "age:" + this. age + "sex" + this. sex );
}
// Call text. call is equivalent to call test.
// Note: obj1 inherits the test function and its attributes and methods.
Test. call (obj1, "Cao Huan", 21 );
Alert (obj1.name );
Obj1.getInfo ();
Obj1.get ();

 

// Call method implementation inheritance
/* Function Parent (name)
{
This. name = name;
This. getParentInfo = function ()
{
Document. write ("name:" + this. name + "<br/> ");
}
}
 
Function Child (password)
{
Parent. call (this, "Cao Huan ");
This. password = password;
This. getChildInfo = function ()
{
Document. write ("name:" + this. name + ", password:" + this. password + "<br/> ");
}
}
 
Var p = new Parent ("");
// P. getParentInfo ();
Var c = new Child ("123456 ");
// C. getParentInfo ();
// C. getChildInfo ();

 

// The third method of Object Inheritance: apply and call
Function Parent (name)
{
This. name = name;
This. getInfo = function ()
{
Document. write ("name:" + this. name, "<br/> ");
}
}
 
Function Child (password)
{
Parent. apply (this, ["Cao Huan"]); // the difference between this and call is that the second parameter is passed as an array, while the call parameter is transmitted as parameters.
This. password = password;
This. getChildInfo = function ()
{
Document. write ("name:" + this. name + ", password:" + this. password, "<br/> ");
}
}
 
Var p = new Parent ("");
// P. getInfo ();
Var c = new Child ("password ");
// C. getInfo ();
// C. getChildInfo ();

 

// Implement inheritance in the prototype chain mode. The defect is that parameters cannot be passed to the constructor.
Function ParentPrototype (){}
ParentPrototype. prototype. name = "James ";
ParentPrototype. prototype. getInfo = function ()
{
Document. write ("name:" + this. name + "<br/> ");
}
Function ChildPrototype (){};
// The Key to integration is the code.
ChildPrototype. prototype = new ParentPrototype ();
ChildPrototype. prototype. password = "password ";
ChildPrototype. prototype. getAllInfo = function ()
{
Alert ("asd ");
Document. write ("name:" + this. name + ", password:" + this. password );
}
 
Var child = new ChildPrototype ();*/
// Child. getInfo ();
// Child. getAllInfo ();
// Prototype chain inheritance method that can pass parameters (composite): This method is recommended.
 
Function ParentFh (name)
{
This. name = name;
}
ParentFh. prototype. getInfo = function ()
{
Document. write ("name:" + this. name, "<br/> ");
}
 
Function ChildFh (name, password)
{
This. password = password;
ParentFh. call (this, name );
}
ChildFh. prototype = new ParentFh ();
ChildFh. prototype. getAllInfo = function ()
{
Document. write ("name:" + this. name + ", password:" + this. password );
}
Var parentFh = new ParentFh ("Cao Huan ");
ParentFh. getInfo ();
Var childFh = new ChildFh ("Cao Juan", "password ");
ChildFh. getAllInfo ();


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.