1. Function call (similar to calling method)
functioncallsomefunction (someFunction, someargument) {returnsomeFunction (someargument); } functionadd10 (num) {returnNum + 10; } varRESULT1 = Callsomefunction (ADD10, 10);//call ADD10 to pass parameter 10 to ADD10 alert (RESULT1); // - functiongetgreeting (name) {return"Hello," +name; } varRESULT2 = Callsomefunction (getgreeting, "Nicholas"); alert (RESULT2); //Hello, Nicholas.
2. function return function
functioncreatecomparisonfunction (PropertyName) {return function(Object1, object2) {varValue1 =Object1[propertyname]; varvalue2 =Object2[propertyname]; if(Value1 <value2) { return-1; } Else if(Value1 >value2) { return1; } Else { return0; } }; } vardata = [{name: "Zachary", age:28}, {name: "Nicholas", age:29}]; Data.sort (Createcomparisonfunction ("Name") ); The//sort function receives a function as a sort reference, and the function createcomparisonfuntion returns an anonymous sort function alert (data[0].name);//NicholasData.sort (Createcomparisonfunction ("Age")); Alert (data[0].name);//Zachary
3, apply () method use
functionsum (NUM1, num2) {returnNUM1 +num2; } functioncallSum1 (NUM1, num2) {returnSum.apply ( This, arguments);//sum function request to pass CALLSUM1 's pointer to oneself, and make the calculation, at this time this point to CallSum1}functioncallSum2 (NUM1, num2) {returnSum.apply ( This, [Num1, num2]); } alert (CallSum1 (10,10));// -Alert (callSum2 (10,10));// -
4, Function arguments caller use
function outer () { inner (); } function inner () { alert (inner.caller); } Outer ();
Caller
Returns a reference to the function that called 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 of the call function body } outer ();
functionfactorial (num) {if(Num <= 1) { return1; } Else { returnnum * Arguments.callee (num-1)//callee The current function's reference is the function body of the factorial function itself}}varTruefactorial =factorial; Factorial=function(){ return0; }; Alert (Truefactorial (5));// -Alert (factorial (5));//0
6. Funtion bind () method
Window.color = "Red" ; var o = {color: "Blue" }; function Saycolor () {AL ERT ( this .color); var objectsaycolor = Saycolor.bind (o); Objectsaycolor (); // blue /* bind is mainly to change the inside of the function of this point, which is added after the ECMA5, so IE8 the browser does not support the Bind method A new function is created, called a binding function. When this binding function is called, the binding function takes the first parameter of the Bind method as this when it is created, and the second and subsequent arguments to the Bind method, plus the arguments of the binding function itself, call the original function in order as the parameters of the original function. */
7, Function Call () method
Window.color = "Red" ; var o = {color: "Blue" }; function Saycolor () {alert (
this
.color); } saycolor ();
//
red
this ); // red this point to the window saycolor.call (window); // red ibid. saycolor.call (o); // blue at this point, the pointer to Saycolor is pointing to O
function sum (NUM1, num2) { return num1 + num2; } function callsum (NUM1, num2) { return sum.call (This, NUM1, num2); } Alert (Callsum (10,10)); // -
8 Length of function
function Sayname (name) {alert (name); function sum (NUM1, num2) { return num1 + num2; function Sayhi () {AL ERT ( "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
JavaScript Learning Summary (v) Function object