JS Call and apply

Source: Internet
Author: User

JS's call and apply

Call and apply are the more important two methods in JS, generally used in the framework and component design, such as jquery Code. So what do these two methods do, and here's what we'll look at in code:

1 function f () {2       console.log (this.name), 3  }4 5  var obj = {};6  obj.name = "obj"; 7 8  F.call (obj);// Obj

F () outputs this.name, where this refers to the caller of the function at runtime (for this in JS, refer to: JS scope and the This keyword), so the result of the F () output depends on the object that called it.

Obj is a common object in JavaScript, obj.name = "obj";  is to add an attribute name to obj and assign the value "obj". In general, only obj can access its own properties name,f why can I access obj.name?

The role of the call method is reflected here:

F.call (obj) translates into vernacular: The execution space of the Obj object is called F (), which is equivalent to OBJ.F (), when this in F refers to obj, this.name equals obj.name, so Output "obj".

This is the simplest explanation for the call method.

Call has a brother. Apply, the only difference is that the transfer parameter form is different, call (Obj,args.) can pass a parameter list, apply (obj,args[]) only pass an array parameter.

Of course, the two brothers have other functions, that is, the inheritance in JS.

JS does not have extend keywords such as Java, C # and other high-level languages, so there is no concept of inheritance in JS, if you must inherit, call and apply can achieve this function:

1 function Animal (name,weight) {2        this.name = name; 3        this.weight = weight; 4   } 5  6   function Cat () {7< C6/>animal.call (this, ' cat ', ' 50 '); 8        //animal.apply (this,[' cat ', ' []]) 9        This.say = function () {            console.log ("I am" + this.name+ ", my Weight is "+ this.weight"),        }13   }14   var cat = new Cat (); Cat.say   ()//i am cat,my weight is 50

From the output, Cat does inherit the attributes in Animal, and the key to inheritance is Animal.call (this, ' Cat ', ' 50 ')!

When executing cat (), it is called animal (), which is the equivalent of this in the execution space of the call object this. Animal (). This. When Animal () is executed, the name and weight properties are generated on this, and the caller of the Say method is this, so this.name in Say () is the Name property in Animal. So the output is the animal property value.

var cat = new Cat ();

After this sentence is executed, this is cat, Cat.say () is equivalent to This.say ().

JS 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.