JS Apply Application

Source: Internet
Author: User
JS apply method details 1Add to favoritesArticle,Add to favorites Posted four months ago (). 305 Total times of reading 2 Comments: JS apply method details

At the beginning, I saw the JavaScript Functions apply and call, which were very vague and hard to understand. Recently, I saw some articles on the Internet showing examples of the apply method and call method, finally, I have a little eye off. Here I will take the following notes and hope to share them with you .. if there is anything wrong or the law is unclear, I hope the readers can give more comments to improve them together ..

I want to solve the following problems:

1. What is the difference between apply and call?

2. When to use apply and call

3. Other clever use of apply (usually when can I use apply)

I first found the definitions of apply and call on the Internet, and then explained the meaning and how to use these two methods using examples.

Apply: the method that can hijack another object and inherit the attributes of another object.

The function. Apply (OBJ, argS) method can receive two parameters.
OBJ: this object will replace this object in the function class.
ARGs: This is an array and it will be passed as a parameter to function (ARGs --> arguments)

Call: it means the same as apply, except that the parameter list is different.

Function. Call (OBJ, [param1 [, param2 [,... [, Paramn])
OBJ: this object will replace this object in the function class.
Params: this is a list of parameters.

1. Apply example:

<SCRIPT type = "text/JavaScript">/* defines a human */function person (name, age) {This. name = Name; this. age = age;}/* defines a student class */functionstudent (name, age, grade) {person. apply (this, arguments); this. grade = Grade;} // create a student var student = new student ("Qian", 21, ""); // test alert ("name:" + student. name + "\ n" + "Age:" + student. age + "\ n" + "grade:" + student. grade); // you can see the test result name: Qian age: 21 grade: Grade 1 // I didn't assign a value to the name and age attributes in the student class, why are the values of these two attributes? This is the magic of apply. </SCRIPT>

Analysis: person. Apply (this, arguments );

This: student is used to create an object.

Arguments: An array, that is, ["Qian", "21", "First Grade"];

In other words, student is used to execute the content in the person class. This exists in the person class. in this way, the attributes are created in the student object.

 

2. Call example

In the studen function, you can change the fix in apply to the following:

Person. Call (this, name, age );

So OK.

3. When to use apply and call

When an object parameter is given, if the parameter is in the form of an array, for example, the arguments parameter is passed in the apply example, this parameter is of the array type, in addition, when you call person, the parameter list is consistent (that is, the first two parameters of person and student are consistent). You can use apply, if my person parameter list is like this (age, name), and student's parameter list is (name, age, grade), you can use call to implement it, that is, directly specify the location (person. call (this, age, name, grade ));

4. Some other clever use of apply

Careful people may have noticed that when I call the apply method, the first parameter is the object (this), and the second parameter is an array set,

When calling person, he does not need an array, but why does he give me an array? I can still resolve the array as a parameter.,

This is a clever use of apply. You can convert an array into a parameter list by default ([param1, param2, param3] To param1, param2, param3) if we useProgramIt may take a while to replace every item in the array with a list of parameters. With this feature of apply, we have the following efficient methods:

 

A) math. Max can be used to obtain the largest entry in the array.

Because the math. max parameter does not support math. Max ([param1, param2]), that is, the array

However, it supports math. Max (param1, param2, param3 ...), Therefore, we can solve var max = math. Max. Apply (null, array) according to the feature of apply, so that we can easily obtain the largest item in an array.

(Apply will replace a number assembly with a parameter followed by a parameter and pass it to the method.)

This is the first parameter given a null value during the call. This is because there is no object to call this method. I only need to use this method to help me calculate the result and get the returned result ,. therefore, a NULL is passed directly.

B) math. Min can be used to obtain the smallest entry in the array.

The same idea as Max is Var min = math. Min. Apply (null, array );

C) array. Prototype. Push can merge two arrays.

Similarly, the push method does not provide an array of push, but it provides push (param1, Param ,... Paramn) so you can also use apply to replace this array, that is:

Vararr1 = new array ("1", "2", "3"); vararr2 = new array ("4", "5", "6"); array. prototype. push. apply (arr1, arr2 );

It can also be understood that arr1 calls the push method, and the parameter is a set of parameters that are assembled into parameter lists by applying.

Under what circumstances can I use apply for special usage such as math. Min:

Generally, the target function only needs n parameter lists, instead of receiving an array ([param1 [, param2 [,... [, Paramn]), you can use the apply method to cleverly solve this problem!

5. Conclusion:

At first, I had no idea about apply. I finally read it several times and I typed it several times.CodeSo, no matter what you do, as long as you are willing to move your mind and work on the code, this technology will master...

For example, the fourth part cleverly solves the problems that actually exist. This is definitely not a solution that beginners can think of (this is not what I think ), if you do not have a certain understanding of programming, you will not think of this. In a single sentence, accumulating and learning more, improving your abilities and understanding of programming ideas are the most critical!

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.