- 相關推薦
javascript之Function對象學習小結
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ù)接收一個函數(shù)作為排序的參考,函數(shù)createComparisonFuntion返回了一個匿名的排序函數(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ù)請求把callSum1的指針傳給自己,并作計算,此時的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//返回一個對函數(shù)的引用,該函數(shù)調(diào)用了當前函數(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當前函數(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指向,這個是在ECMA5以后加入的,所以IE8一下的瀏覽器不支持 bind方法會創(chuàng)建一個新函數(shù),稱為綁定函數(shù).當調(diào)用這個綁定函數(shù)時,綁定函數(shù)會以創(chuàng)建它時傳入bind方法的第一個參數(shù)作為this, 傳入bind方法的第二個以及以后的參數(shù)加上綁定函數(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 此時this指向window sayColor.call(window); //red 同上 sayColor.call(o); //blue 此時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ù)的長度
【javascript之Function對象學習小結】相關文章:
javascript克隆對象深度介紹07-25
JavaScript中的三種對象10-24
JavaScript學習筆記08-24
關于javascript對象之內(nèi)置和對象Math的使用方法10-08
IE與Firefox在JavaScript上的不同寫法小結07-22
JavaScript高級程序設計:本地對象Array10-22