Javascript prototype operation notes

Source: Internet
Author: User

CopyCode The Code is as follows: // var people = {name: "Xiong", age: 15 };
// Var person = function (user, age ){
// This. Name = user;
// This. Age = age;
// This. Say = function () {alert ("I am" + this. Name + "\ n" + this. Age );}
//}
// Var Chairman = function (name, salary ){
// Person. Call (this, name );
//}
// Var bill = new person ("bill", 15 );
// Var Hu = new chairman ("Hu Jintao ");
// Person. Prototype. Eat = function (){
// Alert ("I'm eating ");
//}
// Bill. Eat ();
Function person (name) // base class Constructor
{
This. Name = Name;
};

Person. Prototype. sayhello = function () // Add method to prototype of the base class Constructor
{
Alert ("Hello, I'm" + this. Name );
};

Function employee (name, salary) // subclass Constructor
{
Person. Call (this, name); // call the base class Constructor
This. Salary = salary;
};

Function Xiong (name, age ){
Employee. Call (this, name );

}

Employee. Prototype = new person (); // create a base class object as the prototype of the subclass prototype. It is very interesting here

Xiong. Prototype = new employee ();

Employee. Prototype. showmethemoney = function () // Add prototype to constructor to subclass
{
Alert (this. Name + "$" + this. Salary );
};
VaR billgates = new person ("Bill Gates"); // create a billgates object for the base class person
VaR stevejobs = new employee ("Steve Jobs", 1234); // create the stevejobs object of the subclass employee
VaR hiakuotiankong = new Xiong ("sea sky ");
VaR benbenxiong = new Xiong ("stupid bear ");

// Billgates. sayhello (); // call the prototype method directly through the object
// Hiakuotiankong. sayhello (); // directly call the prototype method of the base class through the subclass object. Follow the instructions!
Benbenxiong. sayhello = function () {// masks the prototype sayhello method.
Alert ("Haha, I'm" + this. Name );
}
Benbenxiong. sayhello ();
// Stevejobs. showmethemoney (); // directly call the prototype method of the subclass through the subclass object
// Alert (billgates. sayhello = stevejobs. sayhello); // display: True, indicating that the prototype method is shared.
Xiong. Prototype. Goodbye = function (){
Alert (this. Name + "bye-bye ");
}
Benbenxiong. Goodbye ();

In JavaScript, prototype not only allows objects to share their wealth, but also allows prototype to search for the original
Nature, so that the heritage of the elders can be passed on from generation to generation. When you read a property from an object or call a method
If this property or method does not exist, it will search for its associated prototype object. If prototype does not exist, it will
Search for prototype associated with its predecessor prototype until the process of finding or tracing ends.

In JavaScript, object attributes and Method Tracing mechanisms are implemented through the so-called prototype chain. When new
When the operator constructs an object, it also assigns the prototype object of the constructor to the newly created object to become the original
Type object. The built-in prototype object of the object should be invisible to the outside world, although some browsers (such as Firefox) can allow us to access this
Built-in prototype object, but this is not recommended. The built-in prototype object itself is also an object and also has its own associated prototype object.
Formed the so-called prototype chain.

At the very end of the prototype chain, it is the prototype object pointed to by the prototype property of the object constructor. This prototype object
Is the oldest ancestor of all objects. This ancestor implements methods that are inherent to all objects such as tostring. Other built-in
The prototype of constructors, such as function, Boolean, String, date, and Regexp, are inherited from this ancestor.
But they define their own attributes and methods, so that their children and grandchildren will show the characteristics of their respective clan.

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.