The _javascript techniques of JavaScript inheritance

Source: Internet
Author: User
Crap
I was very impressed by the manager asking one of the questions during the interview because I hadn't been able to answer it for a long time. The question is how does JavaScript implement inheritance? Object-oriented is in the development process has been used, therefore, the inheritance is also the most basic part of the beginning of their contact with JS to now almost two years, this foundation I unexpectedly did not pass, it seems that my theoretical work to enhance the strength of AH!!! I looked up the data and finally got a deep understanding. There's so much nonsense, coding Action ...
Body
As we all know, C # is used in traditional class inheritance is very simple, but in JS, it is not so simple, because it uses the prototype (prototype) inheritance, to achieve a relatively complex.
Copy Code code as follows:

Defining People objects
var people = function () {};
People.prototype = {
height:175,
Walk:function () {
Alert ("People are walking ...");
}
}
Define Me Object
var Me = function () {};
Set the prototype property of Me to the People object
Me.prototype = new People ();
Refer to me for creating a reference to the Me object
Me.prototype.constructor = Me;
Modify Height Property
Me.prototype.SetHeight = function (v) {
Me.prototype.Height = v;
}
Add Stop Action
Me.prototype.Stop = function () {
Alert ("I ' m Stop.");
}
var m = new Me ();
The result is people are 175cm tall.
Alert ("People are" + m.height + "cm tall.");
M.setheight (185);
The result is I ' m 185cm tall.
Alert ("I ' m" + m.height + "cm tall.");
The result is people are walking ...
M.Walk ();
The result is I ' m Stop.
M.stop ();
var y = new Me ();
Results for you ' re 185cm tall.
Alert ("You ' re" + y.height + "cm tall.");

As you can see from the example above, the Me object inherits the People object, can access the properties and actions of the people prototype, and can have the actions and properties of me. Of particular note is that in the example above, a y=new me () is instantiated, but when the Height property is displayed, it is not originally 175, but is 185 modified by the M instance, so the new Me () does not create a people implementation, but rather repeats the instance on its prototype. So in the above example, all of the Me objects share the same height attribute, which should be paid special attention to in the use of inheritance.
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.