Home  >  Article  >  Web Front-end  >  Introduction to JavaScript Function function types

Introduction to JavaScript Function function types

高洛峰
高洛峰Original
2017-01-04 16:13:311730browse

// In JS, the Function (function) type is actually an object; each function is an instance of the Function type; and has the same properties and methods as other reference types;

// Because A function is an object, so the function name is actually a pointer to the function object;

1. Declaration method of function

1.函数声明方式
  function box(num1,num2){
    return num1+num2;
  }
 
2.函数表达式定义函数
  var box = function(num1,num2){  // 通过变量box即可引用函数;
    return num1+num2;
  };                  // 注意函数末尾有一个分号,就像声明其他变量时一样;
  var another = box;         // 使用不带圆括号的函数名是访问函数指针;而非调用函数;
    console.log(another(10,10));  
3.使用Function构造函数
  var box = new Function('num1','num2','return num1+num2');
// 第三种方式不推荐,这种语法会导致解析两次代码(第一次解析常规JS代码,第二次解析传入构造函数中的字符串),从而影响性能;
// 可以通过这种语法来理解"函数是对象,函数名是指针"的概念;

2. Function as value

// JS中的函数名本身就是变量,所以函数也可以作为值来使用;
// 也就是说,不仅可以像传参数一样把一个函数传递给另一个函数,而且可以将一个函数作为另一个函数的结果返回;
  function box(sumFunction,num){    // 无论第一个参数传递进来的是什么函数,
    return sumFunction(num);     // 它都会返回执行第一参数后的结果;
  }
  function sum(num){
    return num+10;
  }
  // 传递函数到另一个函数里;
    // 要访问函数的指针不执行函数的话,须去掉函数名后的圆括号;
  var result = box(sum,10);      // =>20;

3. Internal function Attributes

// 函数内部有两个特殊的对象:arguments和this;
 
// 1.arguments:是一个类数组对象,包含着传入函数中的所有参数,主要用途是保存函数参数;
// arguments这个对象还有一个名叫callee的属性,该属性是一个指针,指向拥有这个arguments对象的函数;
  function box(num){
    if(num<=1){
      return 1;
    }else{
      return num*arguments.callee(num-1);  // 使用arguments.callee来执行box本身;
    }
  }
 
// 2.this:引用的是函数据以操作的对象,或者说函数调用语句所处的作用域;
// 当在全局作用域调用函数时,this对象引用的就是window;
  window.color = "red";
  alert(this.color);        // 打印全局的color;=>red;
  var box = {
    color:&#39;blue&#39;,
    sayColor:function(){
      alert(this.color);    // 打印局部的color;=>blue;
    }
  };

Four function attributes and methods

// JS中的函数是对象,因此函数也有属性和方法;包含length和prototype;
 
// length属性:表示函数希望接收到命名参数的个数;
  function box(name,age){
    alert(name+age);
  }
  alert(box.length);        // 2s
 
// prototype属性:它是保存所有实例方法的真正所在,也就是原型;
// prototype包含两个方法:apply()和call(),每个函数都包含这两个非继承而来的方法;
// 这两个方法的用途都在特定的作用域中调用函数,实际上等于设置函数体内this对象的值;
  var color = &#39;red&#39;;
  var box = {
    color = &#39;blue&#39;;
  }
  function sayColor({
    alert(this.color);
  });
  sayColor();           // 作用域在window;
  sayColor.call(this);      // 作用域在window;
  sayColor.call(window);     // 作用域在window;
  sayColor.call(box);       // 作用域在box,对象冒充;=>red;
// 使用call(box)方法的时候,sayColor()方法的运行环境已经变成了box对象里了;
// 使用call()或apply()来扩充作用域的最大好处,就是对象不需要与方法发生任何耦合关系;
// 耦合:相互关联的意思,扩展和维护会发生连锁反应;
// 也就是说,box对象和sayColor()方法之间不会有多余的关联操作,比如:box.sayColor = sayColor;
 
  function Animal(){  
    this.name = "Animal";  
    this.showName = function(){  
      alert(this.name);  
    }  
  }  
  function Cat(){  
    this.name = "Cat";  
  }  
  var animal = new Animal();  
  var cat = new Cat();  
  //通过call或apply方法,将原本属于Animal对象的showName()方法交给对象cat来使用。  
  //输入结果为"Cat"  
  animal.showName.call(cat,",");  
  //animal.showName.apply(cat,[]);

Five summary
1 // Functions are actually instances of the Function type, so functions are also objects; and this is the most distinctive feature of JavaScript place;
2 // Because of the function object, the function also has methods that can be used to enhance its behavior;


More JavaScript Function function type introduction related articles Please pay attention to PHP Chinese website!


Statement:
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