Principle of JavaScript inheritance

Source: Internet
Author: User
Through the Operation Analysis of Several JavaScript Functions, we can draw a conclusion:
Javascript inheritance is simulated by initializing objects.
CodeI. Implementation of Inheritance
< Script Language = " Javascript " Type = " Text/livescript " >
Function person ()
{
This. Say=Function (){Alert ("Name")};
}
Function persona ()
{
This. Say=Function (){Alert ("Windowsay");};
}
Persona (); // How to add a window object
Window. Say (); // Show windowsay
VaR me = {} ;
Person. Call (me );
Me. Say (); // The output result is name. We can conclude that me inherits the say method through person. Call (me ).
</ Script >
Comparison:
< Script Language = " Javascript " Type = " Text/livescript " >
Function person ()
{

}
Person. Say = Function () {Alert ("Name")} ;
VaR me = {} ;
Person. Call (me );
// Me. Say (); // Output object doesn' t support this method or property! We can see that me does not have the "inherit" Person's say Method
</ Script >
Why?
The reason is that when the first method is used to execute person. Call (me), specify this in person as me, and then execute the code in the person method.
Obviously, This . Say = Function () {Alert ("Name")} This statement adds a method to me. Let me objects have a say behavior.
While in persona ();, You need to execute the code in Persona. At this time, this object represents the window, so the window obtains the say action.
The second method executes the internal code of person, so it does not add behavior to me. Calling me. Say () naturally fails.

In this case, call, apply, For (Property In   Object ), Prototype and other methods to implement inheritance, it is only a simulation of Inheritance
Method. The specific process is to dynamically add functions and attributes to an object.

Person. Call (me); is the property method for adding person to me, which function person () {Alert ("Name");} Who is the target?
We use examples to illustrate
< Script Language = " Javascript " Type = " Text/livescript " >
Window. Name = " Window " ;
Function person ()
{
Alert (This. Name );
}
Window. Person (); // Display window
Person (); // Display window
</ Script >
We can see that the function defined in the script tag is actually an operation on the window object. We have added methods and properties to the window object.

Let's take a look.
< Script Language = " Javascript " Type = " Text/livescript " >
Function person (thing)
{
This. Say=Function (){Alert (thing );};
}
VaR me = {} ;
Person. Call (Me, " Thing " );
Me. Say (); // Output thing
Window. me. Say (); // Output thing
// Window. Person. Say ("person "); // Output object doesn' t support this method or property!
</ Script >
We can see that the object me is defined in the script tag and is also a variable of the window object. At the same time, it obtains the copy of the say method through call.
While "person" is used as a method, although a say method is defined in it, and "person" does not get the copy of "say", which can be used to enable "person "?
New Person (), or person. Call (person ). The new process is the process of setting this in person as person and adding behavior.
As follows:
< Script Language = " Javascript " Type = " Text/livescript " >
Function person (thing)
{
This. Say=Function (){Alert (thing );};
}
VaR me = {} ;
Person. Call (Me, " Thing " );
Me. Say (); // Output thing
Window. me. Say (); // Output thing
Person. Call (person, " Person " );
Person. Say (); // Output person
Window. Person. Say (); // Output person
VaR P = New Person ( " Person " );
P. Say (); // Output person
Window. Person. Say ( " Person " ); // Output person
</ Script >

OriginalArticle, Reprinted, please indicate the source!
All copyright reserved!

 

Home: http://jingtao.cnblogs.com

QQ: 307073463
Email: jingtaodeemail@qq.com
MSN: sunjingtao@live.com

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.