Transferred from: http://www.dewen.org/q/433
Method One:
- var a=[1,2,3,5];
- Alert (Math.max.apply (null, a));//maximum value
- Alert (Math.min.apply (null, a));//minimum value
Multidimensional arrays can be modified like this:
- var a=[1,2,3,[5,6],[1,4,8]];
- var ta= a.join (","). Split (",");//convert to one-dimensional array
- Alert (Math.max.apply (null,ta));//maximum Value
- Alert (Math.min.apply (null,ta));//minimum Value
Explanation Reference: http://blog.csdn.NET/qianzai5765638/article/details/6952321
===========================================================
Js Apply Method Detailed
Mainly to solve a few problems:
1. Where is the difference between apply and call?
2. Under what circumstances use apply, and under what circumstances call
3. Other clever uses of apply (generally under what circumstances you can use Apply)
I first looked at the definition of apply and call from the internet, and then used examples to explain the meaning of the two methods and how to use Them.
Apply: methods can hijack another Object's method, inheriting another object's Properties.
Function.apply (obj,args) method can receive two parameters
Obj: This object will replace the This object in the function class
Args: This is an array, which is passed as a parameter to function (args-->arguments)
Call: same as apply, except that the argument list is not the Same.
Function.call (obj,[param1[,param2[,... [, paramn]]])
Obj: This object will replace the This object in the function class
Params: This is a list of parameters
1. Apply Example:
- <script type="text/javascript">
- /* Define a human */
- function Person (name,age) {
- this.name=name;
- this.age= age;
- }
- /* define a student class */
- Functionstudent (name,age,grade) {
- Person.apply (this,arguments);
- this.grade=grade;
- }
- Create a Student class
- var student=New student ("qian", 21, "first grade");
- Test
- Alert ("name:" +student.name+ "\ n" + "age:" +student.age+ "\ n" + "grade:" +student.grade);
- You can see the test results Name:qian age:21 grade: First Grade
- I did not assign a value to the name and age attribute in the student class, Why is there the value of these two properties, this is the magic of apply.
- </Script>
Analysis: person.apply (this,arguments);
This: in the creation of the object at this time represents the student
Arguments: is an array, i.e. ["qian", "21", "first grade"];
That is, the popular point is: use student to execute the contents of the person class, in the person class inside the existence of this.name and other statements, so that the property is created into the student object
2. Call Example
In the Studen function can be modified into the following: Person.call (this,name,age); That's Ok.
3. Under what circumstances use apply, and under what circumstances call
In the case of an object parameter, if the argument is in the form of an array, such as the parameter arguments is passed in the Apply example, the parameter is an array type, and the list of parameters is consistent when the person is called ( That is, the first two bits of the parameter list of the person and student are Consistent) can be applied, if my Person's parameter list is such (age,name), and student's argument list is (name,age,grade), This can be done with call, that is, directly specify the position of the corresponding value of the parameter list (person.call (this,age,name,grade));
4. Some other clever uses of apply
The attentive person may have noticed that when I invoke the Apply method, The first argument is the object (this), The second argument is an array collection, and when you call person, it is not an array, But why would he give me an array? I can still parse the array into one parameter , which is a clever use of apply, you can convert an array by default to a parameter list ([param1,param2,param3] to param1, Param2,param3) this, If we use the program to implement each item of the array as a list of parameters, it may take a while, with the use of this feature, so there are the following efficient methods:
A) Math.max can be implemented to get the largest item in the array
Because the Math.max parameter does not support Math.max ([param1,param2]), which is the array
But it supports Math.max (param1,param2,param3 ...), so you can solve the Var max=math.max.apply (null,array) according to the characteristics of the just apply. This makes it easy to get the largest item in an array (apply will change a number assembly to a parameter by passing it to a METHOD)
This block in the call when the first parameter gave a null, this is because there is no object to call this method, I just need to use this method to help me to calculate the return of the results on the LINE. so it passed directly a null past
B) Math.min can be implemented to get the smallest item in the array
Likewise and Max is a thought var min=math.min.apply (null,array);
C) Array.prototype.push can implement two array merging
The same push method does not provide an array of push, but it provides push (param1,param,... Paramn) so it is also possible to replace this array with apply, namely:
- var arr1=New Array ("1", "2", "3");
- var arr2=New Array ("4", "5", "6");
- Array.prototype.push.apply (arr1,arr2);
It is also possible to understand that ARR1 invokes the push method, which is a set of parameters that can be assembled into a parameter list by using Apply.
Under what circumstances, you can use a special usage like math.min and so on:
In general, the target function requires only n argument lists, not the form of an array ([param1[,param2[,... [, paramn]]] ), you can solve this problem skillfully by apply way!
5. Summary:
At first I do not understand the apply very much, the last to see a few times, I have to knock a few times the code, only to understand the middle of the truth, so, no matter what to do, as long as they are willing to move the brain, willing to strike code, such a technology will be mastered ...
There are, for example, the fourth part of the content, ingenious solution to the real problem, this is certainly not a beginner can think of the solution (this is not my own thinking), do not have a certain understanding of programming will not think of this, or a sentence, more accumulation, more study, The most important thing is to improve your ability and understanding of programming ideas!
JS gets the maximum and minimum values in the array (with multidimensional Arrays)