JS method call and apply instance parsing

Source: Internet
Author: User

There are two very special methods for implementing inheritance in JS Programming, call and apply.

In ECMAScript v3, these two methods are defined for a function prototype, both of which work the same way: using these two methods can invoke functions like other object methods, which is copied from the book, at least I do not understand what this means.
The following is easy to understand, first look at the paragraph code:

function introduce (name,age) {document.write ("My name is" +name+ ". I am "+var p=New  people (); Introduce.call (P,

Say the above code, with the call, introduce has become the method of P, do not know that you understand it?

Using the call method, the code above is equivalent to this code:

functionthis. Name=this. age=. introduce=function() {document.write ("My name is" +name+ ". I am "+

Do you understand the meaning? Apply is also the same effect.

Well, we don't care what this method actually does in practice, it's about grammar first.
Call accepts at least one parameter, and the first parameter of the invocation refers to the object you need, such as the example above, the introduce method expects him to be called by the object P, then p is used as the first parameter of the calling. The number of arguments remaining is arbitrary and acts as a parameter to the introduce method. Order in the order in which the introduce parameters are declared. For example Introduce.call (p, "windking", 20), if introduce is an example method of P, then that is: P. Introduce ("Windking", 20). Do you get it?  Remember that the order in which parameters are passed in is consistent with the order of the function declaration parameters. Www.jbxue.com
Understanding the Call,apply method is easy to understand, the only difference between apply and call is that call accepts at least one parameter, and apply takes only two parameters, and the first parameter is the same as call, and the second argument is a labeled set, such as Introduce.call (p, "windking", 20) can be rewritten as introduce.apply (p,["windking", 20]). Did you get it this time?
So what is the use of these two methods? Wouldn't it be better to implement the introduce as a people if we were just trying to achieve the above function?

I summarize the application into two articles:
1. Shared methods. Look at the code first:

function introduce (name,age) {document.write ("My name is" +name+ ". I am "+

This is a self-introduction method, now suppose there is a boy class, and a girl's class (here I just want to demonstrate, in practice, will use a people of the parent class), because their introduce are the same, so we can share this method.

functionthis. boyintroduce=function() {introduce.call ( This

Similarly, the same is true in girl, so that we can avoid writing code. In fact, this is a bit far-fetched, because we can completely write:

functionthis. boyintroduce=function

But at this point, we look a lot simpler if we use apply:

functionthis. boyintroduce=function() {introduce.apply ( This

Isn't it a lot simpler? If there are many parameters, then it is not necessary to write such a string of dense parameters!

2. Cross-domain Call www.jbxue.com
A simple example (demo only, no value):

functionthis. boyintroduce=function() {document.write ("My name is" +name+ ". I am "+function

This is a boy and a girl class, and then we write the following code:

var b=new Boy ("windking");b.boyintroduce ();

There's no objection. Suppose one day a girl also want to do a self-introduction, just by accident, then I do not need to modify the girl class, because the other girls are shy, do not like to introduce themselves. Then I can do it this time.

var g=New Girl ("Xuan", +); Introduce.call (g,"Xuan", 22);

3, real use--inheritance

Here is the most extensive application of call and apply, which is used to construct inheritance.

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.