JS 函数的属性和方法

函数是 JavaScript 中特殊的对象,可以拥有属性和方法,就像普通的对象拥有属性和方法一样。

甚至可以用 Function() 构造函数来创建新的函数对象。


属性

[ length 属性 ]

在函数中,arguments 对象的 length 属性,表示实参个数,而函数的 length 属性,则表示形参个数。

function add(x,y){
    console.log(arguments.length)   // 3
    console.log(add.length);   // 2
}
add(1,2,3);

[ name 属性 ]

函数定义了一个非标准的name属性,通过这个属性可以访问到给定函数指定的名字,这个属性的值永远等于跟在function关键字后面的标识符,匿名函数的name属性为空。

//IE11-浏览器无效,均输出undefined
//chrome在处理匿名函数的name属性时有问题,会显示函数表达式的名字
function fn(){};
console.log(fn.name);//'fn'
var fn = function(){};
console.log(fn.name);//'',在chrome浏览器中会显示'fn'
var fn = function abc(){};
console.log(fn.name);//'abc'

name属性早就被浏览器广泛支持,但是直到ES6才将其写入了标准:ES6对这个属性的行为做出了一些修改。

  • 如果将一个匿名函数赋值给一个变量,ES5 会返回空字符串,而ES6的name属性会返回实际的函数名
var func1 = function () {};
func1.name //ES5:  ""
func1.name //ES6: "func1"
  • 如果将一个具名函数赋值给一个变量,则ES5和ES6的name属性都返回这个具名函数原本的名字
var bar = function baz() {};
bar.name //ES5: "baz"
bar.name //ES6: "baz
  • Function构造函数返回的函数实例,name属性的值为“anonymous”
(new Function).name // "anonymous"
  • bind返回的函数,name属性值会加上“bound ”前缀
function foo() {};
foo.bind({}).name // "bound foo"
(function(){}).bind({}).name // "bound "

[ prototype 属性 ]

每个函数都有一个prototype属性,该属性指向一个对象的引用,这个对象称做原型对象( prototype object )。

每个函数都包含不同的原型对象。将函数用做构造函数时,新创建的对象会从原型对象上继承属性。

function fn(){};
var obj = new fn;
fn.prototype.a = 1;
console.log(obj.a);  // 1

方法

[ apply() 和 call() ]                       // 每个函数都包含两个非继承而来的方法:apply()和call()。

这两个方法的用途都是在特定的作用域中调用函数,实际上等于函数体内this对象的值。

要想以对象o的方法来调用函数f(),可以这样使用call()和apply()

f.call(o);
f.apply(o);

假设o中不存在m方法,则等价于:

o.m = f; // 将f存储为o的临时方法 
o.m();  // 调用它,不传入参数 
delete o.m;  // 将临时方法删除

代码示例:

window.color = "red";
var o = {color: "blue"};
function sayColor(){
    console.log(this.color);
}
sayColor();            //red
sayColor.call(this);   //red
sayColor.call(window); //red
sayColor.call(o);      //blue

apply()接收两个参数:一是在其中运行函数的作用域(或者可以说成是要调用函数的母对象,它是调用上下文,在函数体内通过this来获得对它的引用),二是参数数组,它可以是Array的实例,也可以是arguments对象。

   function sum(num1, num2){
       return num1 + num2;
   }

// 因为运行函数的作用域是全局作用域,所以this代表的是window对象
   function callSum1(num1, num2){
       return sum.apply(this, arguments);
   }
   function callSum2(num1, num2){
       return sum.apply(this, [num1, num2]);
   }
   console.log(callSum1(10,10));//20
   console.log(callSum2(10,10));//20

call() 与 apply() 的作用相同,它们的区别仅仅在于接收参数的方式不同。对于call()而言,第一个参数是this值没有变化,变化的是传递给函数的参数必须逐个列举出来。

function sum(num1, num2){
    return num1 + num2;
}
function callSum(num1, num2){
    return sum.call(this, num1, num2);
}
console.log(callSum(10,10));   //20

至于是使用apply()还是call(),完全取决于采取哪种函数传递参数的方式最方便。如果打算直接传入arguments对象,或者包含函数中先接收到的也是一个数组,那么使用apply()肯定更方便;否则,选择call()可能更合适。

[ call() 和 apply() 的应用 ]

  • 调用对象的原生方法
var obj = {};
obj.hasOwnProperty('toString');// false
obj.hasOwnProperty = function (){
  return true;
};
obj.hasOwnProperty('toString');// true
Object.prototype.hasOwnProperty.call(obj, 'toString');// false
  • 找出数组最大元素

JS 不提供找出数组最大元素的函数,结合使用 apply 方法和 Math.max 方法,就可以返回数组的最大元素。

var a = [10, 2, 4, 15, 9];
Math.max.apply(null, a);//15
  • 将类数组对象转换成真正的数组
Array.prototype.slice.apply({0:1,length:1});//[1]

或者

[].prototype.slice.apply({0:1,length:1});//[1]
  • 将一个数组的值 push 到另一个数组中
var a = [];
Array.prototype.push.apply(a,[1,2,3]);
console.log(a);//[1,2,3]
Array.prototype.push.apply(a,[2,3,4]);
console.log(a);//[1,2,3,2,3,4]

如果使用ES6中的不定参数则非常简单

var a  = [...[1,2,3],...[2,3,4]];
console.log(a);//[1,2,3,2,3,4]

[ bind() ]                                     // IE8- 浏览器不支持

bind()是ES5新增的方法,这个方法的主要作用就是将函数绑定到某个对象。

当在函数 f() 上调用 bind() 方法并传入一个对象 o 作为参数,这个方法将返回一个新的函数。

function f(y){
    return this.x + y; //这个是待绑定的函数
}
var o = {x:1};//将要绑定的对象
var g = f.bind(o); //通过调用g(x)来调用o.f(x)
g(2);//3

兼容代码

Function.prototype.bind = function(context){
  var self = this;
  return function(){
    return self.apply(context,arguments);
  }
}

通常,会把它实现得稍微复杂一点,使得可以填入一些参数

Function.prototype.bind = function(context){
  var self = this,
      context = [].shift.call(arguments),
      args = [].slice.call(arguments);
  return function(){
    return self.apply(context,[].concat.call(args,[].slice.call(arguments)));
  }
}

bind() 方法不仅是将函数绑定到一个对象,它还附带一些其他应用:除第一个实参外,传入bind()的实参也会绑定到this,这个附带应用是一种常见的函数式编程技术,即'柯里化?!'。

var sum = function(x,y){
    return x+y;
}
var succ = sum.bind(null,1);
succ(2); //3,x绑定到1,并传入2作为实参y
function f(y,z){
    return this.x + y + z;
}
var g = f.bind({x:1},2);
g(3); //6,this.x绑定到1,y绑定到2,z绑定到3

使用 bind() 方法实现柯里化可以对函数参数进行拆分

function getConfig(colors,size,otherOptions){
    console.log(colors,size,otherOptions);
}
var defaultConfig = getConfig.bind(null,'#c00','1024*768');
defaultConfig('123');//'#c00 1024*768 123'
defaultConfig('456');//'#c00 1024*768 456'

[ toString() ]

toString()方法返回函数代码的字符串,而静态toString()方法返回一个类似'[native code]'的字符串作为函数体

function test(){
    alert(1);//test
}
test.toString();/*"function test(){
                    alert(1);//test
                  }"*/
Function.toString();//"function Function() { [native code] }"

// 函数的 toLocaleString() 方法 和 toString() 方法返回的结果相同


[ valueOf ]  函数的 valueOf() 方法返回函数本身

function test(){
    alert(1);//test
}
test.valueOf();/*function test(){
                    alert(1);//test
                  }*/
typeof test.valueOf();//'function'
Function.valueOf();//Function() { [native code] }