Use of call and apply

Source: Internet
Author: User

Feel that call and apply are inherited, can point to a different function to pass arguments, invoke methods, see the Code

    Function F1 () {
this.p= "1---";
This. A=function (ARG) {
Alert (THIS.P+ARG)
}
}
function F2 () {
this.p= "2---";
This. B=function (ARG) {
Alert (THIS.P+ARG)
}
}
instantiate the function of F1
var fn1=new f1 ();
var fn2=new f2 ();
call the A method, use apply to point to the instantiated object fn1, pass the parameters
Fn1. A.apply (fn1,["This is apply (FN1)]) //1---1
call the A method, use apply to point to the instantiated object fn2, pass the parameters
Fn1. A.apply (fn2,["This is apply (FN2)]) //2---1

Call the B method to point to Fn1, the instantiated object, and pass the parameters
Fn2. B.call (FN1, "This was call (FN1)")
Call the B method to point to Fn2, the instantiated object, and pass the parameters
Fn2. B.call (FN2, "This was call (FN2)")

Use of call and apply

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.