JS inherits the understanding __js

Source: Internet
Author: User

Today again see JS inheritance, with a new understanding. is actually a further understanding of the scope

First look at an example:

function A (name) {
THIS.name = name;
This.sayhello = function () {alert (this.name+ "Say hello!");

function B (name,id) {
This.temp = A;
This.temp (name); Equivalent to New A ();
Delete this.temp; Prevents the properties and methods of superclass a from being overwritten with temp references at a later time
This.id = ID;
This.checkid = function (ID) {alert (this.id==id)};

This is the classic object posing, and the two lines of red are equivalent to executing a (name), and this in a is already replaced with this in B.

So what happens when we change the red part directly to A (name)?

So we do the experiment

var B = new B ("B", "BID");

alert (b.name);//undefined


Because there is no binding scope when executing a, this is still window

We output alert (window.name) or alert (name)//b

So this object is actually a transformation of this, the binding scope to an object

But is there any other way to do this?

Apply and call

The role of personal understanding: calling functions and binding scopes

That's the meaning of the object pretending.

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.