奇怪的关闭行为

发布于 2024-09-07 23:10:07 字数 576 浏览 5 评论 0原文

由于某种原因,以下代码的行为并不像我预期的那样——可能是由于我对它应该如何行为的误解。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请别遗忘我 2024-09-14 23:10:07

这是他们添加的 $.proxy() 的内容1.4 :),像这样:

var contentPane = widget.children("div.content").first();

var success = $.proxy(function (data, successCode, httpRequest) {
                       this.innerHTML = data;
              }, contentPane);

这只是使常见情况的闭包声明变得更短, $.proxy(函数,whatThisIs)


对于“怎么了?”部分...什么都没有,您确定您正在检查正确的变量吗? this 指的是函数内的 window,但 content 是您想要的 jQuery 对象...您可以在此处查看快速测试:http://jsfiddle.net/vhcde/

This is what they added $.proxy() for in 1.4 :), like this:

var contentPane = widget.children("div.content").first();

var success = $.proxy(function (data, successCode, httpRequest) {
                       this.innerHTML = data;
              }, contentPane);

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 to window inside your function, but content is the jQuery object you want...you can see a quick test here: http://jsfiddle.net/vhcde/

蓦然回首 2024-09-14 23:10:07

问题可能出在您的选择器上。在此页面上运行时,以下内容运行良好

var contentPane = $("#header")[0];

var success = (function (content) {
    console.info(content);
    return function (data, successCode, httpRequest) {
        console.warn(content);
        content.innerHTML = data;
    };
})(contentPane); 

success("ohai");

* 为了更加清晰,我在函数定义周围添加了括号。

The problem may likely be your selector. The following works fine when run on this page

var contentPane = $("#header")[0];

var success = (function (content) {
    console.info(content);
    return function (data, successCode, httpRequest) {
        console.warn(content);
        content.innerHTML = data;
    };
})(contentPane); 

success("ohai");

* I've added parenthesis around the function definition for added clarity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文