What is the difference between the Apply () method and the call () method in JavaScript?

Source: Internet
Author: User

JavaScript is one of the must-have technologies for Web front-end development , and will be involved in the process of writing the front end. But in the process of writing JavaScript, there are some ways to be curious, such as the Apply () and the call () method.

The existence is reasonable, many times in programming, we have to use the Apply () and call () method of the scene, the following we will look at the code to see the application of the two methods.

<body>

<script>

var product = "House";

var boss = {

Chooseworker:function (makefunction) {

Return MakeFunction (2);

},

Make:function (count) {//In contrast to the make method of Foodworker and Carworker

Console.log ("Boss does not work");

}

};

var foodworker = {

Product: "Food",

Make:function (count) {

Console.log ("Foodworker:make" + Count + "" + this.product);

}

};

var carworker = {

Product: "Car",

Make:function (count) {

Console.log ("Carworker:make" + Count + "" + this.product);

}

};

Boss.chooseworker (Foodworker.make);

Boss.chooseworker (Carworker.make);

</script>

<body>

In this code, to achieve the goal is: by passing the Worker object's make method to let boss choose to produce that product, boss of the Chooseworker method of the parameter is the Make method. The parameters that would have been expected to be passed to the Chooseworker method would produce food if the Foodworker make method, and if Carworker make method, produce carworker. The expected result is this:

Foodworker:make 2 Food

Carworker:make 2 car

But the result is this:

Foodworker:make 2 House

Carworker:make 2 House

So why is it that the method of specifying the object is passed, and the this.product inside is "house"?

The reason is that the JavaScript object's function itself does not contain information about the object's information, and if you call makefunction directly, its caller defaults to the Window object, which means This==window, so this.product is "house". To achieve the desired effect, you have to use the Apply method. such as this:

<body>

<script>

var product = "House";

var boss = {

Chooseworker:function (makefunction, worker) {

Return makefunction.apply (worker, [2]);

},

Make:function (count) {

Console.log ("Boss does not work");

Www.maizitime.com

}

};

var foodworker = {

Product: "Food",

Make:function (count) {

Console.log ("Foodworker:make" + Count + "" + this.product);

}

};

var carworker = {

Product: "Car",

Make:function (count) {

Console.log ("Carworker:make" + Count + "" + this.product);

}

};

Boss.chooseworker (Foodworker.make, Foodworker);

Boss.chooseworker (Carworker.make, Carworker);

</script>

<body>

The output results are expected, and the apply and call methods are often used for some design patterns and inheritance. The main problem is to solve the point of this object, but you can also replace the caller of the method by using apply and call.

In JavaScript, apply and call function basically the same, the only difference is that apply has two parameters, while call has only one parameter. The first argument to apply is the call object of the function, and the second argument is the argument list (the parameter array) of the function. Of course, if the function has no parameters, you can pass an empty array ([]) to the Apply method; call has only one parameter, which is the calling object of the function. Apply includes the function of call, so it is generally done with apply.

This is the basic use of JavaScript's apply () and call () methods, which are carefully scratched when you look up JavaScript-related material on the web (for more JavaScript knowledge points, see theJavaScript video tutorial "), I hope we all learn together and make progress together.

What is the difference between the Apply () method and the call () method in JavaScript?

Related Article

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.