相关疑难解决方法(0)

mozilla的绑定函数问题

有一个关于我在Mozilla网站上找到的绑定函数实现的问题.在大多数情况下,它对我有意义,但我无法弄清楚这个检查是什么...

this instanceof nop ? this : ( obj || {} ) 
Run Code Online (Sandbox Code Playgroud)

在绑定函数中.显然它检查'this'是否为空函数,但为什么需要绑定空函数.我在firebug中试过它,它有效,但重点是什么?只是想增加我的JavaScript知识,所以任何帮助将不胜感激.

if ( !Function.prototype.bind ) {

  Function.prototype.bind = function( obj ) {

    var slice = [].slice,
    args = slice.call(arguments, 1), 
    self = this, 
    nop = function () {}, 
    bound = function () {
      return self.apply( this instanceof nop ? this : ( obj || {} ), 
                          args.concat( slice.call(arguments) ) );    
    };

    nop.prototype = self.prototype;

    bound.prototype = new nop();

    return bound;
  };
}
Run Code Online (Sandbox Code Playgroud)

javascript

20
推荐指数
1
解决办法
1185
查看次数

标签 统计

javascript ×1