The use and difference between the call method and the Apply method in JavaScript

Source: Internet
Author: User

For the first time in the blog park above the blog, the knowledge because of reading the time found some interesting knowledge, by the way to check the information, sent to the blog, I hope that a little help.

Reading the Book of Advanced JavaScript Programming for several days, I gradually discovered some magical points of knowledge that I had never used or even seen before. Today, when reading about the properties and methods of functions, a little more advanced, so, look at a lot of other people's blog, here to summarize the following aspects of these two methods:

1. Function and usage of call () and apply () 2. When to use Apply (), when to use call ()

The book mentions that each function contains two non-inherited methods: Apply and call, their role is to bind the function to another object to run, that is, the purpose of these two methods is to call the function in a particular scope, when called, set the function body the value of this object. First, show them how to use them:

1 fun.apply (thisarg[, Argsarray]); 2 fun.call (thisarg[, arg1[, arg2[, ...]);

As you can see, thedifference between apply and call is that the second parameter is different. Apply is an array or arguments object. And call is any type separated by commas. to illustrate the use of these two methods more visually, let's first cite an example:

1<script type= "Text/javascript" >2Window.color = "Red";3     varColorset = {4Color: "Gray"5     }6     functionShowcolor () {7Alert This. color);8     }9Showcolor ();//RedTen  OneShowcolor.call ( This);//Red AShowcolor.call (window);//Red -Showcolor.call (Colorset);//Gray -</script>

Here we introduce the concept of this pointer: in JavaScript, the this pointer represents the owner of the object executing the current code. In non-strict mode, the function is called without specifying an environment object, and the this pointer value does not transition to window. When executing line 9th of the above code, where the execution environment is the global environment, the object owner is window, and the Window object has a color property, the result is red. As for lines 11 and 12, the window is displayed. The next 13 lines are the key to the above code, and when you run Showcolor.call (colorset), the execution environment of the function is different, and the this pointer in the body of the function points to the object Colorset, The result is the color property of the Colorset.

This article mainly refer to the blog:

http://blog.csdn.net/myhahaxiao/article/details/6952321

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.