1. Find the largest element in a numeric array (using the Match.max function)
1 var a=[123,23432,345,3,34]; 2 console.log (Math.max.apply (null, a));
2. Convert a numeric array into a function array (each function pops up the corresponding number)
1 var a=[123,23432,345,3,34]; 2 a=a.map (function(value) {3 returnfunction() { 4 alert (value); 5 }; 6 }); 7 Console.log (A[3] ());
3. Sort an object array (sorting criteria is the number of attributes per element object)
varArr=[{A:1,b:2,c:3}, {a:1,b:2,c:3,d:4,e:5}, {a:1,b:2}, {a:1,b:2,c:3,d:4}];object.prototype.proplength=(Object.prototype.hasOwnProperty (' _count_ ')?function (){ return This. _count_;//FF } : function(){ varP,count=0; for(pinch This){ if( This. hasOwnProperty (P)) {Count++; } } returncount;});functionCompare (obj1,obj2) {returnObj1.proplength ()-obj2.proplength ();} Console.log (Arr.sort (Compare));
4. Use JavaScript to print out the number of Fibonacci (without using global variables)
1 function Fibo (n) {2 var self=Arguments.callee; 3 if (n<2) {4 return N; 5 }6 return self (n-1) +self (n-2); 7 }8 Console.log (Fibo (6));
5, to achieve the following syntax functions: var a = (5). Plus (3). Minus (6); 2
1number.prototype.plus=function(a) {2 return This. ValueOf () +A;3 };4number.prototype.minus=function(a) {5 return This. ValueOf ()-A;6 };7 varA = (5). Plus (3). Minus (6);8Console.log (a);
6, to achieve the following syntax function: var a = Add (2) (3) (4); 9
The answer comes from the Gaubee in-depth understanding of the JavaScript series (20): Do you really understand JavaScript? Answer in a detailed answer.
1 functionAdd (num) {2 varself=Arguments.callee;3num+=~~Self ;4self.num=num;5 returnSelf ;6 }7 //valueof is for operation and console use, ToString is for alert8add.valueof=add.tostring=function(){returnAdd.num;};9 varA = Add (2) (3) (4);TenConsole.log (a);
Welcome criticism.
Six-way JavaScript quiz questions