Uncle Tom 6-way JavaScript Study questions

Source: Internet
Author: User

1. Returns the maximum value in the array; (Math.max) First, clear usage. Math.max (1,2,4,-5,3) compares the values passed in the parameter, the number of arguments arbitrary; implementation: with Apply (object,[]); var num =[1,2,3,4,5];var b=math.max.apply (math,num); 2. Converts a numeric array into a function array (each function pops up the corresponding number)
function tofunction (num) {
var mynum= new Array ();
for (Var i=0,len=num.length;i<len;i++)
{
var myfunction= (function (n) {
return function () {
alert (n);
}
}) (Num[i]); The self-executing function, which passes each value in num as a parameter to the function, returns a function that alert the argument each time. Because a closure is used (a free variable, not a variable defined in its own scope), the value of each num "i" is stored by N to implement function functions;
Mynum.push (MyFunction);
}
return mynum;
}
var num=[1,2,3,4,5];
var newnum=tofunction (num);
NEWNUM[4] ();
3. Sort an object array (sorting criteria is the number of attributes per element object)  Num[{a:1,b:2},{},{a:3}]   
Num.sort ()     arrayobj.sort (sortfunction)  
Parameters
Arrayobj
Required option. Any Array object.
Sortfunction
Options are available. is the name of the function used to determine the order of the elements. If this parameter is omitted,
The elements are then sorted in ascending order of ASCII characters.
Description
The sort method sorts the Array objects appropriately;
A new Array object is not created during execution.

If you provide a function for the sortfunction parameter, the function must return one of the following values:

A negative value, if the first argument passed is smaller than the second one.
0, if two parameters are equal.
Positive value if the first argument is larger than the second argument.  for (Var i in object|[]) is unordered when iterating over an object, and I is equivalent to a reference to one of the properties, and the array is ordered by element
function Sortfun (obj1,obj2) {

var fn=function (obj) {
var size=0;//size must be defined within the function, if defined outside the function, each time the function is called, the resulting return size is equal, and does not reach the number of judgments and exchange effect;
for (var i in obj)
{
if (Obj.hasownproperty (i))
size++;
}
return size;
}
RETURN fn (obj1)-FN (OBJ2);
}
var objnum=[
{A:1,b:3,c:4},
{A:1,b:3,c:4,d:5,f:6},
{A:1,b:3,c:4,d:5}
]
Objnum.sort (Sortfun);
Use JavaScript to print out the number of Fibonacci (without using global variables)
function Fibnum (num) {
var fibarr=new Array ();
if (num<2) {
for (Var i=0;i<num;i++)
{
Fibarr.push (i)
}
}
else{
Fibarr.push (0,1);
for (Var i=2;i<num;i++)
{
var fibn=fibarr[i-1]+fibarr[i-2];
Fibarr.push (FIBN);
}
}
return Fibarr;
}

The following syntax functions are implemented: var a = (5). Plus (3). Minus (6);
Number.prototype.plus = function (i) {//Add method plus and minus for its prototype
return this + i;
}
Number.prototype.minus = function (i) {
return this-i;
}

var a = (5). Plus (3). Minus (6);
Console.log (a);
The following syntax functions are implemented: var a = Add (2) (3) (4);
function Add (N1) {
return function (n2) {
N2=N2+N1;
return function (n3)
{
return n3+n2;
}
}

}//topic Understanding is not comprehensive enough, not just the 3 arguments passed in, but in many cases can be debugged successfully
Implementation analysis: Recursive call function () {a+b}
function Myadd (l) {
var myl=l;
var inadd= function (m) {
Myl + = m;
return inadd;
}//when the last parameter is passed in, execution is completed to get the desired value myl, but the function returns Inadd this
Inadd.tostring=inadd.valueof =function () {///When a function or object is returned, the browser makes a conversion of toString or valueOf, We rewrite the functions of the two auto-transform mechanism of the intrinsic function so that it returns the last added value;
return myl;
}
return inadd;
}

var a = Add (2) (3) (4);
Console.log (a);

Uncle Tom 6-way JavaScript Study questions

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.