对原型绑定感到困惑,此声明 [英] Confused on prototype bindings, this statement

查看:74
本文介绍了对原型绑定感到困惑,此声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我想知道上下文如何绑定到this:

In the following code, I'm wondering how context is bound to this:

obj.myMethod();中,上下文被赋予对象.因此,将其记录为对象.

In obj.myMethod();, context is given to the object. So logging it gives the object.

var myFun = obj.myMethod;中,然后在myFun();中,将上下文提供给窗口.

In var myFun = obj.myMethod; then myFun();, context is given to the Window.

唯一的区别是您将函数设置为变量.

The only difference is you're setting the function to a variable.

    var obj = {

        myMethod : function () {
            console.log(this); //'this' is bound to context of object
        }
    };

    obj.myMethod(); //calling object property directly = Object {myMethod: function}

    var myFun = obj.myMethod;   
    myFun(); //but setting obj method property to a variable and running gives Window as context



此melonJS教程之后,我很困惑如何使用此回调(向下滚动到第2部分:加载我们的关卡,您将看到完整的代码)

Following this melonJS tutorial, I'm confused how this callback is used (Scroll down to Part 2: Loading our level, you will see complete code)

// Set a callback to run when loading is complete.
me.loader.onload = this.loaded.bind(this);

我阅读了有关回调的本教程,所以我了解他们的用途...但是我不明白.它说this.loaded.bind(this)

I read this tutorial on callbacks, so I understand what they're used for... But I don't understand. It says this.loaded.bind(this)

第一个和第二个this语句之间有什么区别?他们不一样吗?为什么我需要先呼叫this然后呼叫.loaded.bind()然后再次传递this?

What is the difference between this first and second this statements? Aren't they the same? Why do I need to call this then .loaded.bind() then pass in this again?

因此,在您的示例中,您说我可以通过执行var bindMe = obj.myMethod.bind(obj);来保持上下文,在这种情况下,由于您已经在对象game中,因此您正在使用this?那么this是指所有者game?

So, in your example, you say I can keep context by doing var bindMe = obj.myMethod.bind(obj);, and in this case, you're using this because you're already within the object game? So this refers to the owner game?

谢谢

推荐答案

简短答案

在表达式中

Short answer

In the expression

obj.f()

f中的

this将绑定到obj的值(.左侧的表达式).

this within f will be bound to the value of obj (the expression on the left hand-side of the .).

如果函数是单独"调用的,即

If a function is invoked "alone", i.e.

f()

然后将f中的this绑定到全局对象(在浏览器中,窗口中).

then this within f is bound to the global object (in the browser, window).

也就是说,您可以先使用.bind函数设置上下文,即

That said, you can set the context before hand using the .bind function, i.e.

var g = obj.f.bind(obj);
f(); // success! this == obj

c.f. https://developer.mozilla.org/en -US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

您可能还想看看.call.apply函数.

You might also want to take a look at the .call and .apply functions.

关键点:函数不携带上下文. obj.f是成员访问权限,它所做的只是从obj返回属性f的值.当您调用该函数时,将在全局范围中使用f中的obj.f()f()调用该函数,从而设置上下文.在这种情况下,上下文将是全局对象.

Key point: functions DO NOT carry a context around. obj.f is a member access, all it does is return the value of the property f from obj. The context is set when you CALL the function, either with obj.f() or f() in f exists in the global scope, in which case the context would be the global object.

阅读规格! :) http://www.ecma-international.org/ecma-262/5.1/#sec-10.3

这篇关于对原型绑定感到困惑,此声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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