久久久久无码精品,四川省少妇一级毛片,老老熟妇xxxxhd,人妻无码少妇一区二区

javascript之Function對(duì)象學(xué)習(xí)小結(jié)

時(shí)間:2024-07-21 06:31:39 JavaScript 我要投稿
  • 相關(guān)推薦

javascript之Function對(duì)象學(xué)習(xí)小結(jié)

  1、Function 函數(shù)調(diào)用(類似call方法)

javascript之Function對(duì)象學(xué)習(xí)小結(jié)

  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)文章:

javascript面向?qū)ο笾械膶?duì)象怎么理解09-02

javascript克隆對(duì)象深度介紹07-25

JavaScript中的三種對(duì)象10-24

使用ajax操作JavaScript對(duì)象的方法09-28

JavaScript學(xué)習(xí)筆記08-24

關(guān)于javascript對(duì)象之內(nèi)置和對(duì)象Math的使用方法10-08

IE與Firefox在JavaScript上的不同寫法小結(jié)07-22

JavaScript高級(jí)程序設(shè)計(jì):本地對(duì)象Array10-22

Javascript中arguments對(duì)象的詳解和使用方法08-20

常用排序算法之JavaScript實(shí)現(xiàn)代碼段06-04