Binding 绑定函数的ES5垫片是Javascript

Binding 绑定函数的ES5垫片是Javascript,binding,shim,Binding,Shim,下面是一个用于JS绑定的ES5垫片。我不理解self。请在绑定函数中应用。 我知道如何使用apply方法,但在这种情况下,self指向哪里?它应该是一个 函数,但在这里,self看起来像一个对象 if ( !Function.prototype.bind ) { Function.prototype.bind = function( obj ) { var slice = [].slice, args = slice.call(arguments

下面是一个用于JS绑定的ES5垫片。我不理解self。请在绑定函数中应用。 我知道如何使用apply方法,但在这种情况下,self指向哪里?它应该是一个
函数,但在这里,self看起来像一个对象

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 || {} ), // self in this line is supposed  
        to // represent a function ?
        args.concat( slice.call(arguments) ) );
        };

        nop.prototype = self.prototype;
        bound.prototype = new nop();
        return bound;
        };
  }

请记住,在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 || {} ), // self in this line is supposed  
        to // represent a function ?
        args.concat( slice.call(arguments) ) );
        };

        nop.prototype = self.prototype;
        bound.prototype = new nop();
        return bound;
        };
  }
你就在这里:

self=这个


因此,self并不代表任何东西,self就是实例。

请记住,在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 || {} ), // self in this line is supposed  
        to // represent a function ?
        args.concat( slice.call(arguments) ) );
        };

        nop.prototype = self.prototype;
        bound.prototype = new nop();
        return bound;
        };
  }
你就在这里:

self=这个


因此,self并不代表任何东西,self就是一个实例。

self
正在您列出的垫片中使用,以适应
随范围变化而变化的事实。在Function.prototype.bind函数
的直接作用域内,此
将引用调用绑定函数的对象

一旦您进入嵌套的
绑定
函数的范围此已更改;因此,作者在
bind
函数中分配了
self=this
,以允许调用
bind
this
的值通过词法作用域(闭包)保持对
bind
函数可用

JavaScript中的作用域可能变得相当复杂;有关详细说明,请参阅本文


self
正在您列出的垫片中使用,以适应
随范围变化而变化的事实。在Function.prototype.bind函数
的直接作用域内,此
将引用调用绑定函数的对象

一旦您进入嵌套的
绑定
函数的范围此已更改;因此,作者在
bind
函数中分配了
self=this
,以允许调用
bind
this
的值通过词法作用域(闭包)保持对
bind
函数可用

JavaScript中的作用域可能变得相当复杂;有关详细说明,请参阅本文


我知道这在当时是一个古老的问题,但在谷歌搜索分析中,这个问题出现得相当高。公认的答案相当误导。我已经发布了一个新的答案,还有一些额外的细节。如果你感兴趣的话,请看一看。我知道这是一个古老的问题,但它在谷歌搜索分析中出现得相当高。公认的答案相当误导。我已经发布了一个新的答案,还有一些额外的细节。如果你感兴趣的话,请看一看。嗨,Bigtuncan。与此同时,我还学到了很多关于JS的知识,包括它的功能性质、面向对象的风格,当然还有JS的作用域和闭包。从这个例子中,我记得我不知道原型对象将被is-object函数继承。但不管怎样,谢谢你的回答。嗨,比古纳坎。与此同时,我还学到了很多关于JS的知识,包括它的功能性质、面向对象的风格,当然还有JS的作用域和闭包。从这个例子中,我记得我不知道原型对象将被is-object函数继承。但无论如何,谢谢你的回答。