JavaScript apply core complexity as simple

Source: Internet
Author: User

Apply is the default method of the front-end JavaScript method, which is used to replace the This object of the currently executing method, to implement inheritance, or to modify the method to use inside the This object;

This time first said inherit:

For example:

First, declare two methods inside the test method, Add,sub:

function Test () {

This.add=function (A, b) {

return a+b;

}

This.sub=function (A, b) {

return a-B;

}

}

Then, we then define a method Test2:

function Test2 () {

Test.apply (this);

}

Next, we generate the instance T2, in the instantiation process Test2 becomes the T2 constructor function, at this time Test2 method inside the This object refers to T2;

var t2=new test2 ();

So now, we can test T2:

T2.add (2,1);

T2.sub (3,2);

The result is not very magical.

JavaScript apply core complexity as simple

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.