理解“这个” goog.bind 和 goog.net.Xhrio.send 的上下文
我对调用以下代码时发生的情况感到有点困惑:
goog.net.XhrIo.send("/welcome", goog.bind(this.handleWelcome, this));
我有一个具有此签名的函数:
myproject.MyClass.prototype.handleWelcome = function(response)
在绑定之前,handleWelcome 的上下文无法访问我的 Javascript 类 myproject.MyClass 的实例字段(可以理解) )。根据此处的信息,我现在有了上下文类实例的。一切都很好。
在我做出改变之前,“这个”的上下文是什么?
请原谅我使用的任何非 Javascript 习惯用法——我对 Java 更熟悉,并且可能使用了一些术语的大杂烩。
编辑
最初,我对传递给回调的参数有一些疑问(在本例中是一个目标类型为 goog.net.Xhrio 的事件),但主要问题是关于此和绑定,所以我删除了切向 q。
I am a little bit confused as to what happen when I call the following code:
goog.net.XhrIo.send("/welcome", goog.bind(this.handleWelcome, this));
I have a function with this signature:
myproject.MyClass.prototype.handleWelcome = function(response)
Before I was binding, the context of handleWelcome did not have access to instance fields of my Javascript class myproject.MyClass (understandably). Following the information here, I now have the context of the class instance. All well and good.
What was the context of "this" before I made the change?
Please excuse any non-Javascript idioms I'm using -- I'm much more familiar with Java and am probably using a hodgepodge of terms.
EDIT
Initially I had some questions about what argument was being passed to the callback (in this case an event with a target of of type goog.net.Xhrio) but the main question is about about this and bind, so I removed the tangential q's.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
goog.bind 相当于 function.prototype.bind,但是第一个参数是要绑定的函数,第二个参数是应该绑定的“this”值,任何其余参数都绑定到函数的形式参数。
JavaScript 具有第一类函数,但它们不会继承地与“this”值绑定,因此除非您绑定它,否则该值取决于它的调用方式:
传统上,如果“this”值未指定、未定义或为 null,则它通常被强制进入全局 this,“窗口”。但是,如果您在 EcmaScript 5 严格模式下运行,则该值保持不变(未指定为“未定义”)。
goog.bind is equivalent to function.prototype.bind, but that the first parameter is the function to be bound to, the second the "this" value that should be bound and any remaining parameters are bound to the functions formal parameters.
JavaScript has first class functions, but they aren't inheritly bound with a "this" value, so unless you bind it, the value depends on how it is called:
Traditionally, if "this" value is unspecified, undefined, or null then it is coerced into the global this, "window" usually. However, if you are running in EcmaScript 5 strict mode, the value remains unchanged (unspecified is "undefined").
这取决于谷歌代码的作用,因为它可以在调用回调时将
this
绑定到某些东西,但它可能是未定义的。也就是说,在谷歌库的某个地方,它有一个事件处理程序,它执行以下操作:
“something_mysterious”可能是
null
,它可能是一些半有趣的对象,或者谁知道呢。It depends on what the google code does, since it could concievably bind
this
to something when it invokes your callback, but it was probably undefined.That is, inside the google library somewhere, it's got an event handler that does something like:
well "something_mysterious" could be
null
, it could be some semi-interesting object, or who knows.this.handleWelcome 中的第一个 this 是对父类(函数)的引用。
第二个 this 是对当前函数的引用;
这是我能解释的最模糊的方式,也许在技术上不是 100% 正确,但应该接近。他们可能持有任何东西,也可能什么都没有。
查看正在发生的情况的最简单方法是将两者传递到控制台并查看其中包含的内容,可能使用 firebug。
希望这有帮助。
the first this in this.handleWelcome is reference to the parent class (function).
the second this is a reference to the current function;
That is the vaguest way I can explain it and maybe is not 100% technically correct, but should be close. They could be holding anything or nothing.
The simplest way to see what is going on is to pass both to the console and see what the contain, using maybe firebug.
Hope this helps.