(js) Firebug 调试导致代码正常工作

发布于 2024-11-02 16:31:37 字数 1628 浏览 1 评论 0原文

请原谅这个尴尬的标题,我会尽力解释我的特殊问题。

我有三段 javascript 代码:

  • 一些自执行代码,它调用我的个人 Ajax 函数并向其传递回调。
  • 检索数据的 ajax 函数本身调用向其传递数据的回调。
  • 回调本身接收数据并将其解析为 n 长度的数组。

需要注意的是,自执行代码和回调函数是在它们自己的闭包中定义的。 ajax 函数是通过导入的命名闭包访问的,我将其定义为 $

我对 JavaScript 还很陌生,并且仍在学习闭包及其作用域。我有理由相信这个问题很可能与此有关。

不管怎样,我的问题与尝试访问那个所谓的填充数组有关。由于我在合适的范围内(或者我相信)将数组定义为解析函数,我认为将项目推入其中应该没有问题。

这是自我执行的:

(function ($){

    //Load stock
var items = [];
var response = $.Ajax("scripts/Lookup.php","GET",parse);

function parse(a){
    for(/*Simplified view*/){
        var item = new $.Item();
        item.name = domStuff.textContent;
        item.description = domStuff.textContent;
        item.price = domStuff.textContent;
        item.id = domStuff.textContent;

        items.push(item);
    }
}

    //Test the length (and verify contents)
    for(var i=0; i < items.length; i++){
        alert(items[i].price);
    }
}($));

这是我的定义,其中包括 Ajax 函数:

var $ = (function(){
var s = {};

    var ajax = function(url,method,callback){
        var a = new XMLHttpRequest();
        a.open(method, url, true);
        a.onreadystatechange = function(){
        if(this.readyState==4){
                callback(a);
            }
    }
    a.send();
    };

    s.Ajax = (function(){
        return ajax;
    }());

return s;
}());

所以证明标题合理的是,当我用 firebug 探测代码时,我可以看到 items 填充了 3 个正确定义的对象通过解析的数据。

然后循环按预期发出 3 次警报。

但是,如果我删除断点并让 firebug 忽略代码,则循环不会执行,我只能假设数组为空。

如果我在测试循环之前alert(items),该代码也可以工作。

Please excuse the awkward title I'll try my best to explain my peculiar problem.

I have three bits of javascript code:

  • Some self executing code which calls my personal Ajax function and passes it a callback.
  • The ajax function itself which retrieves the data calls the callback passing it the data.
  • The callback itself which takes in the data and parses it into an array of n length.

What should be noted is that the self executing code and the callback function are defined within their own closure. The ajax function is accessed through an imported named closure which I defined as $.

I am fairly new to JavaScript and I am still learning about closures and their scopes. I have reason to believe that this problem is probably related to that.

Anyway, my problem relates to trying to access that supposedly populated array. Since I defined the array in a suitable scope (or so I believe) as the parse function I think I should have no problem pushing items into it.

This is self exectuting :

(function ($){

    //Load stock
var items = [];
var response = $.Ajax("scripts/Lookup.php","GET",parse);

function parse(a){
    for(/*Simplified view*/){
        var item = new $.Item();
        item.name = domStuff.textContent;
        item.description = domStuff.textContent;
        item.price = domStuff.textContent;
        item.id = domStuff.textContent;

        items.push(item);
    }
}

    //Test the length (and verify contents)
    for(var i=0; i < items.length; i++){
        alert(items[i].price);
    }
}($));

This is my definitions, which includes the Ajax function:

var $ = (function(){
var s = {};

    var ajax = function(url,method,callback){
        var a = new XMLHttpRequest();
        a.open(method, url, true);
        a.onreadystatechange = function(){
        if(this.readyState==4){
                callback(a);
            }
    }
    a.send();
    };

    s.Ajax = (function(){
        return ajax;
    }());

return s;
}());

So what justifies the title is that when I probe the code with firebug, I can see that items is populated with 3 Objects correctly defined by the parsed data.

The loop then alerts as intended 3 times.

However, if I remove the break points and have firebug ignore the code then the loop doesn't play out and I can only assume that the array is empty.

The code works also if I alert(items) prior to the test loop.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

此岸叶落 2024-11-09 16:31:37

AJAX 正在异步填充项目,这意味着在您的闭包中,第一次执行 items.length 将为零(从原始数组初始化)。随着时间的推移,AJAX 通过调用 parse 方法将填充此数组。

因为您将 items 保存在闭包中,所以您实际上无法从外部访问它 - 这是一个很好的做法,但可能会导致调试问题!而不是 items.push(item) 尝试 alert(item) (任何浏览器,但执行阻塞)或 console.log(item) (仅 firebug),以查看您确实从 AJAX 获取结果,只是不在初始执行中。

AJAX is filling items asynchronously, meaning in your closure, on first execution items.length will be zero (from the original array initialization). Over time, AJAX through calls to your parse method will fill this array.

Because you're keeping items in a closure, you won't actually be able to access it from outside - which is a good practice, but can cause problems with debugging! Instead of items.push(item) try alert(item) (any browser, but execution blocking) or console.log(item) (firebug only) to see that you are, indeed, getting the results back from AJAX just not in initial execution.

天煞孤星 2024-11-09 16:31:37

无论如何,我都不是 JavaScript 专家。然而,我在其他编程语言中遇到过调试已经解决了问题的情况。

在这些情况下,调试已经减慢了代码的运行速度。某些进程 A 需要异步进程 B 在执行超出某个点之前返回一个对象或执行某些操作。在这些类型的场景中最有可能发生的是主线程正在继续或不等待其他例程填充对象。

我希望这有帮助。

I'm not a JavaScript expert by any means. However, I have run into situations on other programming languages where debugging has solved an issue.

In those cases, debugging has slowed the code down enough for it to work. Some process A needs asynchronous process B to return an object or do something before executing beyond a certain point. What is most likely happening in these types of scenarios is that the main thread is continuing or not waiting for the object to be populated by the other routine.

I hope that helps.

剩一世无双 2024-11-09 16:31:37

您的解析函数是异步调用的。在测试之前,您需要等待解析函数被调用。

要么在解析函数中测试结果,要么使用另一个函数作为 ajax 调用的回调。该函数调用parse,然后测试结果。

Your parse function is called asynchronously. You need to wait for the parse function to be called before testing it.

Either test the result in you parse function or have another function as the callback for ajax call. This function calls parse and then test the results.

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