Small series two days have not updated the article, the small partners are not waiting for a hurry, today began to continue our "Everyday JavaScript Learning Summary" series of articles, I hope you continue to pay attention to.
1 function calls (similar to call method)
function Callsomefunction (someFunction, someargument) {return
someFunction (someargument);
}
function Add10 (num) {return
num +
}
var result1 = callsomefunction (ADD10, 10);//Call Add10 pass parameter 10 to ADD10
alert (RESULT1); The
function getgreeting (name) {return
"Hello," + name;
}
var result2 = callsomefunction (getgreeting, "Nicholas");
alert (RESULT2); Hello, Nicholas.
2. function return function
function Createcomparisonfunction (PropertyName) {return
function (Object1, object2) {
var value1 = object1[ PropertyName];
var value2 = Object2[propertyname];
if (value1 < value2) {
return-1;
} else if (value1 > value2) {return
1;
} else {return
0;
}
};
}
var data = [{name: ' Zachary ', age:28}, {name: ' Nicholas ', age:29}];
Data.sort (createcomparisonfunction ("name")); the//sort function receives a function as a reference to the sort, and the function createcomparisonfuntion returns an anonymous sort function
alert (data[0].name); Nicholas
Data.sort (createcomparisonfunction ("Age"));
alert (data[0].name); Zachary
3. Apply () method use
function sum (NUM1, num2) {return
num1 + num2;
}
function CallSum1 (NUM1, num2) {
return sum.apply (this, arguments); The//sum functions request that the pointer to the CALLSUM1 be passed to itself and calculated. This now points to callSum1
}
function callSum2 (NUM1, num2) {return
sum.apply (this, [Num1, num2]);
Alert (CALLSUM1 (10,10));
alert (callSum2 (10,10)); 20
4, Function arguments caller use
function outer () {
inner ();
}
function inner () {
alert (inner.caller);
}
Outer ();
Caller
//Returns a reference to a function that calls the current function.
5, Arguments.callee.caller
function outer () {
inner ();
}
function inner () {
alert (arguments.callee.caller);
Argments.callee is the function body itself, Arguments.callee.caller is the function body's call function body
}
outer ();
function factorial (num) {
if (num <= 1) {return
1;
} else {return
num * Arguments.callee (num-1)//c Allee The current function reference is the function body of the factorial function
}
}
var truefactorial = factorial;
factorial = function () {return
0;
};
Alert (Truefactorial (5));
Alert (factorial (5)); 0
6. Funtion bind () method
Window.color = "Red";
var o = {color: "Blue"};
function Saycolor () {
alert (this.color);
}
var objectsaycolor = Saycolor.bind (o);
Objectsaycolor (); Blue
/
bind is primarily to change this point within the function, which was added after ECMA5, so the IE8 browser does not support the
bind method creates a new function called the binding function. When this binding function is invoked, The binding function takes this as this in the first argument of the Bind method when it is created, and the
second and subsequent parameters of the Bind method, along with the parameters of the run-time itself of the binding function, call the original function in order as an argument to the original function.
*/
7. Function Call () method
Window.color = "Red";
var o = {color: "Blue"};
function Saycolor () {
alert (this.color);
}
Saycolor (); Red
Saycolor.call (this); Red this point points to window
saycolor.call (window);//red Ibid
saycolor.call (o); Blue at this point the Saycolor pointer points
to O function sum (NUM1, num2) {return
num1 + num2;
}
function Callsum (NUM1, num2) {return
Sum.call (this, NUM1, num2);
Alert (Callsum (10,10)); 20
8. Length of function
function Sayname (name) {
alert (name);
}
function sum (NUM1, num2) {return
num1 + num2;
}
function Sayhi () {
alert ("HI");
}
alert (sayname.length); 1
alert (sum.length); 2
alert (sayhi.length); 0
//The actual return is the length of the parameter of the function
The above is today's JavaScript learning Summary, and will continue to update every day, I hope you continue to pay attention.