The code information comes from http://ejohn.org/apps/learn/.
Using variable numbers of parameters is good for programming
function Merge (Root) { for (i = 0; i < arguments.length; i++) for (var inch arguments[i]) return Root[key] = Arguments[i][key]} var merged = merge ({name: "John"}, {city: " Boston "= =" John "," The Name property is the same as the original. " = = "Boston", "the city attribute is copied to the first parameter object");
This method can be used to set the number of positional parameters, its idea is to access the two-dimensional array method to access all objects, all the content copied to the first object.
It is important to note that all incoming parameters exist in the arguments object, and the parameters of the function, in turn, correspond to the arguments that are passed in. Like the subject, arguments includes {name: "John"},{city: "Boston"}; and Root is {name: "John"}.
How do I build a function that finds the largest/smallest number in an array?
function Smallest (array) { returnfunction largest (array) { return Math.max.apply (Math, array);} console.log (Smallest ([0, 1, 2, 3]) = = 0, "Find the minimum position"); Console.log (Largest ([0, 1, 2, 3]) = = 3, "Where to find the maximum value");
A different approach
function smallest () { returnfunction largest () { return Math.max.apply (Math, arguments); } console.log (Smallest (0, 1, 2, 3) = = 0, "Find the location of the minimum value"); Console.log (Largest (0, 1, 2, 3) = = 3, "Where to find the maximum value");
What's wrong with this code?
function Highest () { return arguments.sort (function(A, b) { return B- A; }); } console.log (Highest (1, 1, 2, 3) [0] = = 3, "Get the highest value"); Console.log (Highest (3, 1, 2, 3, 4, 5) [1] = = 4, "View the second element of an array");
Sort is the method of an Array object.
Exercise: Is there any built-in method that can help to convert an array object to arrays?
//tip: The array has the. Slice and. Splice methods that return the new array.functionHighest () {returnMakearray (arguments). Sort (function(A, b) {returnB-A; }); } functionMakearray (array) {//Complement Code} //expected: [3,2,1,1]Console.log (Highest (1, 1, 2, 3) [0] = = 3, "Get Maximum"); //expected: [5,4,3,3,2,1]Console.log (Highest (3, 1, 2, 3, 4, 5) [1] = = 4, "Verification results");
We can use a handy built-in approach
function Highest () { return makearray (arguments). Sort (function(A, b ) { return B- A; function Makearray (array) { return Array (). Slice.call (array);} Console.log (Highest (1, 1, 2, 3) [0] = = 3, "Get maximum Value"); Console.log (Highest (3, 1, 2, 3, 4, 5) [1] = = 4, "Verification results");
Here is the conversion method of the array of classes and arrays, through call, modify the context, invoke slice implementation.
Exercise: Implementing a multiplication function (the first parameter, and the maximum value of the remaining parameter)
function Multimax (Multi) { // var allbutfirst = ___; // find the largest number in the remaining array of arguments var largestallbutfirst = ___; return Multi * Largestallbutfirst;} console.log (Multimax (3, 1, 2, 3) = = 9, "3*3=9 (the first parameter, the maximum value of the remaining parameter )" );
Using call and apply can be a good solution to this problem
function Multimax (Multi) { // var allbutfirst = Array (). Slice.call (arguments,1 ); // find the largest number in the remaining array of arguments var largestallbutfirst = Math.max.apply (math,allbutfirst); return Multi * Largestallbutfirst;} console.log (Multimax (3, 1, 2, 3) = = 9, "3*3=9 (the first parameter, the maximum value of the remaining parameter )" );
JavaScript Advanced Knowledge Analysis--Flexible parameters