奇怪的关闭行为
由于某种原因,以下代码的行为并不像我预期的那样——可能是由于我对它应该如何行为的误解。
var contentPane = widget.children("div.content").first();
var success = function (content) {
return function (data, successCode, httpRequest) {
content.innerHTML = data;
};
}(contentPane);
我已经附加了我的调试器(无论如何,Firebug),它看起来像 content.innerHTML = data;
行上的“内容”是 Window 对象,而我应该是 的结果var contentPane = widget.children("div.content").first();
,正确吗?
请注意,如果我在函数之前设置断点,则 contentPane 确实会设置为我期望的内容(与 div.content
匹配的 jQuery 对象)。这是怎么回事,我错过了什么?
For some reason, the following bit of code isn't behaving as I would anticipate -- likely due to a misunderstanding on my end as to how it should behave.
var contentPane = widget.children("div.content").first();
var success = function (content) {
return function (data, successCode, httpRequest) {
content.innerHTML = data;
};
}(contentPane);
I've attached my debugger (well, Firebug anyway) and it looks like 'content' on the line content.innerHTML = data;
is the Window object, when I should be the result of var contentPane = widget.children("div.content").first();
, correct?
Note that if I set a breakpoint before the function, contentPane is indeed set to what I would expect (a jQuery object matching div.content
). Whats going on here, what am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是他们添加的
$.proxy()
的内容1.4 :),像这样:这只是使常见情况的闭包声明变得更短,
$.proxy(函数,whatThisIs)
。对于“怎么了?”部分...什么都没有,您确定您正在检查正确的变量吗?
this
指的是函数内的window
,但content
是您想要的 jQuery 对象...您可以在此处查看快速测试:http://jsfiddle.net/vhcde/This is what they added
$.proxy()
for in 1.4 :), like this:This just makes the closure declaration for the common case like you have much shorter,
$.proxy(function, whatThisIs)
.For the "what's wrong?" part...nothing, are you sure you're checking the right variable?
this
refers towindow
inside your function, butcontent
is the jQuery object you want...you can see a quick test here: http://jsfiddle.net/vhcde/问题可能出在您的选择器上。在此页面上运行时,以下内容运行良好
* 为了更加清晰,我在函数定义周围添加了括号。
The problem may likely be your selector. The following works fine when run on this page
* I've added parenthesis around the function definition for added clarity.