简体   繁体   中英

Script not printing error in JavaScript Chrome console

I have just spent far too long trying to find a problem with the code below.

In turned out that because of the context that addRoute was called in, keys() was not returning the keys of the results object. To fix this I had to use Object.keys() despite it working without a problem in the JavaScript console (which I later realised was because of the context).

My question is, why didn't this show in my JavaScript console? It took me quite a while to realise (I have cropped the full code, the actual function is a lot bigger).

Wrong, but no error in the console:

Map.prototype.addRoute = function (results) {
    var sectionsIDs = keys(results);
}

Correct

Map.prototype.addRoute = function (results) {
    var sectionsIDs = Object.keys(results);
}

Your first function uses the keys console API function .

That " Command Line API Reference " page includes the warning:

Note: This API is only available from within the console itself. You cannot access the Command Line API from scripts on the page.

Thus, it is by design that the keys function only exists for code run directly on the console.

Chrome gives you a small hint about the keys function being a console-only function if you view it in the console:

 > keys  
 function keys(object) { [Command Line API] }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM