The call () and apply () methods in JS

Source: Internet
Author: User

1.call ()

Syntax:obj1.call (obj2[,param1,param2,...])
definition: use Obj2 object instead of Obj1, call obj1 method. The obj1 will be applied to the OBJ2.
Description:The call method can be used to invoke a method in place of another object. The call method can change the object context of a function from the initial context to a new object specified by Obj2. If the OBJ2 parameter is not provided, then the Global object is used as the obj2.

2.apply ()

Syntax: Obj1.call (Obj2[,arrarg])
Definition: Use Obj2 object instead of Obj1, call obj1 method. The obj1 will be applied to the OBJ2.
Note: Call () is the same as apply (), but call () can receive parameters of any type, and apply () can only receive array parameters.

3. Basic usage[JavaScript]View PlainCopy print?
  1. function Add (A, b) {
  2. return a+b;
  3. }
  4. function sub (c,d) {
  5. return c-d;
  6. }
  7. function result () {
  8. this.addvalue = null;
  9. this.subvalue = null;
  10. this.showresult=function () {
  11. Alert (this.addvalue);
  12. Alert (this.subvalue);
  13. }
  14. }
  15. var r = new result ();
  16. R.addvalue = Add.call (sub,4,2); //6, apply the Add method to the sub, which is the pointer to the sub that points to the Add method
  17. R.subvalue = Sub.call (add,4,2); //2, replacing the sub object with the Add object, and invoking the method of the Sub object
  18. R.showresult (); //In JS The function is also a functions object, the function name is the object reference
4. Inheritance Features[JavaScript]View PlainCopyprint?
  1. function Add (A, b) {
  2. return a+b;
  3. }
  4. function sub (c,d) {
  5. return c-d;
  6. }
  7. function result () {
  8. this.addvalue = null;
  9. this.subvalue = null;
  10. this.showresult=function () {
  11. Alert (this.addvalue);
  12. Alert (this.subvalue);
  13. }
  14. }
  15. var r = new result ();
  16. R.addvalue = Add.call (r,4,2); //6,r inherit all the attributes of the Add function
  17. R.subvalue = Sub.call (r,4,2); //2,r all features of the integrated sub function
  18. R.showresult ();

The call () and apply () methods in JS

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.