The call apply in JS

Source: Internet
Author: User


In JavaScript oop, we often define this:
function Cat () {
}
cat.prototype={
Food: "Fish",
Say:function () {
Alert ("I Love" +this.food);
}
}


var blackcat = new Cat;
Blackcat.say ();

But if we have an object Whitedog = {food: "Bone"}, we do not want to redefine it say method, then we can use call or Apply with Blackcat say method: BlackCat.say.call (Whitedog );

So, you can see that call and apply are meant to change this dynamically, and when an object does not have a method, but the others, we can use call or apply to manipulate other objects.

The DOM node chosen by document.getElementsByTagName is a type of array that is similar to array. It cannot apply methods such as Push,pop under the array. We can do this by:
var domnodes = Array.prototype.slice.call (document.getElementsByTagName ("*"));
This allows the domnodes to apply all the methods under the array.

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
  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 PlainCopy
  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 apply 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.