Home > Web Front-end > JS Tutorial > 每天一篇javascript学习小结(Function对象)_javascript技巧

每天一篇javascript学习小结(Function对象)_javascript技巧

WBOY
Release: 2016-05-16 15:32:04
Original
1119 people have browsed it

小编两天都没有更新文章了,小伙伴们是不是等着急了,今天开始再继续我们的《每天一篇javascript学习小结》系列文章,希望大家继续关注。

1、Function  函数调用(类似call方法)

  function callSomeFunction(someFunction, someArgument){
      return someFunction(someArgument);
    }

    function add10(num){
      return num + 10;
    }
    
    var result1 = callSomeFunction(add10, 10);//调用add10 把参数10传给add10
    alert(result1);  //20
    
    function getGreeting(name){
      return "Hello, " + name;
    }
    
    var result2 = callSomeFunction(getGreeting, "Nicholas");
    alert(result2);  //Hello, Nicholas

Copy after login

2、函数返回函数

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函数接收一个函数作为排序的参考,函数createComparisonFuntion返回了一个匿名的排序函数
    alert(data[0].name); //Nicholas
    
    data.sort(createComparisonFunction("age"));
    alert(data[0].name); //Zachary    

Copy after login

3、apply()方法使用

 function sum(num1, num2){
      return num1 + num2;
    }
    
    function callSum1(num1, num2){
      return sum.apply(this, arguments);//sum函数请求把callSum1的指针传给自己,并作计算,此时的this指向callSum1
    }
    
    function callSum2(num1, num2){
      return sum.apply(this, [num1, num2]);
    }
    
    alert(callSum1(10,10));  //20
    alert(callSum2(10,10));  //20
Copy after login

4、函数Arguments Caller使用

function outer(){
      inner();
    }
    
    function inner(){
      alert(inner.caller);
    }
    
    outer();
caller

//返回一个对函数的引用,该函数调用了当前函数。

Copy after login

5、arguments.callee.caller

function outer(){
      inner();
    }
    function inner(){
      alert(arguments.callee.caller);
      //argments.callee就是函数体本身,arguments.callee.caller就是函数体的调用函数体
    }
    outer();


function factorial(num){
      if (num <= 1) {
        return 1;
      } else {
        return num * arguments.callee(num-1)//callee当前函数的引用即factorial函数本身的函数体
      }
    }

    var trueFactorial = factorial;
    
    factorial = function(){
      return 0;
    };
    
    alert(trueFactorial(5));  //120
    alert(factorial(5));    //0

Copy after login

6、Funtion bind()方法

 window.color = "red";
    var o = { color: "blue" };
              
    function sayColor(){
      alert(this.color);
    }
    var objectSayColor = sayColor.bind(o);
    objectSayColor();  //blue
    /*
      bind主要是为了改变函数内部的this指向,这个是在ECMA5以后加入的,所以IE8一下的浏览器不支持
      bind方法会创建一个新函数,称为绑定函数.当调用这个绑定函数时,绑定函数会以创建它时传入bind方法的第一个参数作为this,
      传入bind方法的第二个以及以后的参数加上绑定函数运行时本身的参数按照顺序作为原函数的参数来调用原函数.
    */
Copy after login

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
Copy after login

8、函数长度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
    //实际返回的是函数的参数的长度
Copy after login

以上就是今天的javascript学习小结,之后每天还会继续更新,希望大家继续关注。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template