- 相關(guān)推薦
javascript之Function對(duì)象學(xué)習(xí)小結(jié)
1、Function 函數(shù)調(diào)用(類似call方法)
function callSomeFunction(someFunction, someArgument){ return someFunction(someArgument); } function add10(num){ return num + 10; } var result1 = callSomeFunction(add10, 10);//調(diào)用add10 把參數(shù)10傳給add10 alert(result1); //20 function getGreeting(name){ return "Hello, " + name; } var result2 = callSomeFunction(getGreeting, "Nicholas"); alert(result2); //Hello, Nicholas
2、函數(shù)返回函數(shù)
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"));//sort函數(shù)接收一個(gè)函數(shù)作為排序的參考,函數(shù)createComparisonFuntion返回了一個(gè)匿名的排序函數(shù) alert(data[0].name); //Nicholas data.sort(createComparisonFunction("age")); alert(data[0].name); //Zachary
3、apply()方法使用
function sum(num1, num2){ return num1 + num2; } function callSum1(num1, num2){ return sum.apply(this, arguments);//sum函數(shù)請(qǐng)求把callSum1的指針傳給自己,并作計(jì)算,此時(shí)的this指向callSum1 } function callSum2(num1, num2){ return sum.apply(this, [num1, num2]); } alert(callSum1(10,10)); //20 alert(callSum2(10,10)); //20
4、函數(shù)Arguments Caller使用
function outer(){ inner(); } function inner(){ alert(inner.caller); } outer();caller//返回一個(gè)對(duì)函數(shù)的引用,該函數(shù)調(diào)用了當(dāng)前函數(shù)。
5、arguments.callee.caller
function outer(){ inner(); } function inner(){ alert(arguments.callee.caller); //argments.callee就是函數(shù)體本身,arguments.callee.caller就是函數(shù)體的調(diào)用函數(shù)體 } outer();function factorial(num){ if (num <= 1) { return 1; } else { return num * arguments.callee(num-1)//callee當(dāng)前函數(shù)的引用即factorial函數(shù)本身的函數(shù)體 } } var trueFactorial = factorial; factorial = function(){ return 0; }; alert(trueFactorial(5)); //120 alert(factorial(5)); //0
6、Funtion bind()方法
window.color = "red"; var o = { color: "blue" }; function sayColor(){ alert(this.color); } var objectSayColor = sayColor.bind(o); objectSayColor(); //blue /* bind主要是為了改變函數(shù)內(nèi)部的this指向,這個(gè)是在ECMA5以后加入的,所以IE8一下的瀏覽器不支持 bind方法會(huì)創(chuàng)建一個(gè)新函數(shù),稱為綁定函數(shù).當(dāng)調(diào)用這個(gè)綁定函數(shù)時(shí),綁定函數(shù)會(huì)以創(chuàng)建它時(shí)傳入bind方法的第一個(gè)參數(shù)作為this, 傳入bind方法的第二個(gè)以及以后的參數(shù)加上綁定函數(shù)運(yùn)行時(shí)本身的參數(shù)按照順序作為原函數(shù)的參數(shù)來調(diào)用原函數(shù). */
7、Function call()方法
window.color = "red"; var o = { color: "blue" }; function sayColor(){ alert(this.color); } sayColor(); //red sayColor.call(this); //red 此時(shí)this指向window sayColor.call(window); //red 同上 sayColor.call(o); //blue 此時(shí)sayColor的指針指向o function sum(num1, num2){ return num1 + num2; } function callSum(num1, num2){ return sum.call(this, num1, num2); } alert(callSum(10,10)); //20
8、函數(shù)長度length
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 //實(shí)際返回的是函數(shù)的參數(shù)的長度
【javascript之Function對(duì)象學(xué)習(xí)小結(jié)】相關(guān)文章:
學(xué)習(xí)JavaScript的7個(gè)理由03-03
常用排序算法之JavaScript實(shí)現(xiàn)代碼段08-29
JavaScript學(xué)習(xí)歷程和心得范文(精選10篇)06-09
常用的JavaScript模式08-29
JavaScript常用方法匯總08-26
播音主持技巧之對(duì)象感04-02
高效編寫JavaScript代碼的技巧08-29
最常用的20個(gè)javascript方法函數(shù)08-29
讀javascript的發(fā)展史讀后總結(jié)02-13
日語學(xué)習(xí)之全然詳解11-18