我的“这个”在哪里?使用对象方法作为回调函数 [英] Where is my 'this'? Using objects method as a callback function

查看:183
本文介绍了我的“这个”在哪里?使用对象方法作为回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于javascript规范的通用问题或函数指针(delegates?)的实现,它们是指向对象方法的。



请阅读以下代码片段。这里我们有一个对象与一个方法使用'this'访问对象字段。当我们像平常一样调用此方法( o.method(); )时,返回对象的指定字段的值。但是当我们创建指向这个方法(callback)的指针并且调用它时,返回一个未定义的值,因为方法范围内的'this'现在是全局对象。

  var o = {
field:'value',
method:function(){
return this.field;
}
};

o.method(); //返回'value'

var callback = o.method;

callback(); //返回'undefined'因为'this'是全局对象

解决方案

this是后期绑定。也就是说,它绑定到一个事情就在函数执行之前。它的约束取决于你如何调用你的函数。



如果你调用它像函数invokation):

  myfunction() 

this绑定到全局对象



如果你像这样调用它(方法调用):

  myobject.myfunction 

this绑定到myobject



您也可以像这样调用它(调用invokation):

  myfunction.call 

在这种情况下,this绑定到myobject



还有(构造函数调用):

  new MyFunction 

其中this绑定到一个新构造的空白对象,其原型是MyFunction.prototype。 / p>

这是javascript的创建者如何谈论它,反正。 (我认为这是在规范中讨论这种方式)不同的方式调用一个函数。



新版本的ecmascript标准(ecmascript5)包括原型库的绑定方法,它返回一个新的函数,thisprebound到你指定的东西。例如:

  mynewfunction = myfunction.bind(myobject); 
mynewfunction();

mynewfunction的调用已将this绑定到myobject。


I have a generic question about javascript specification or implementation of functions pointer (delegates?) which are points to object methods.

Please, read the following code snippet. Here we have an object with a method using 'this' to access an object field. When we call this method as usual (o.method();), returns value of the specified field of the object. But when we create pointer to this method (callback) and invoke it, returns an undefined value, because 'this' inside method's scope now is global object.

var o = {
    field : 'value', 
    method : function() {
        return this.field;
    }
};

o.method(); // returns 'value'

var callback = o.method;

callback(); // returns 'undefined' cause 'this' is global object

So, where is my 'this'?

解决方案

"this" is late binding. that is, it gets bound to a thing just before the function is executed. What it is bound to depends on how you call your function.

if you call it like (function invokation):

   myfunction();

"this" is bound to the global object

if you call it like (method invokation):

   myobject.myfunction();

"this" gets bound to "myobject"

you can also call it like so (call invokation):

   myfunction.call(myobject);

in which case "this" gets bound to myobject

there is also (constructor invokation):

 new MyFunction();

in which "this" gets bound to a newly constructed blank object whose prototype is MyFunction.prototype.

this is how the creators of javascript talk about it, anyway. (and I think it is discussed this way in the spec) Different ways of invoking a function.

the new version of the ecmascript standard (ecmascript5) includes the prototype library's "bind" method, which returns a new function with "this" prebound to something you specify. for instance:

  mynewfunction = myfunction.bind(myobject);
  mynewfunction();

the invokation of mynewfunction has "this" already bound to myobject.

这篇关于我的“这个”在哪里?使用对象方法作为回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆