简体   繁体   English

JavaScript Chrome控制台中的脚本未打印错误

[英]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. 事实证明,由于调用了addRoute的上下文, keys()没有返回结果对象的键。 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). 为了解决这个问题,我不得不使用Object.keys()尽管它在JavaScript控制台中没有问题(后来我意识到是由于上下文)而没有问题。

My question is, why didn't this show in my JavaScript console? 我的问题是,为什么这没有在我的JavaScript控制台中显示? 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 . 您的第一个函数使用keys控制台API函数

That " Command Line API Reference " page includes the warning: 该“ 命令行API参考 ”页面包括警告:

Note: This API is only available from within the console itself. 注意:此API仅在控制台本身内可用。 You cannot access the Command Line API from scripts on the page. 您无法从页面上的脚本访问命令行API。

Thus, it is by design that the keys function only exists for code run directly on the console. 因此,根据设计, keys功能仅适用于直接在控制台上运行的代码。

Chrome gives you a small hint about the keys function being a console-only function if you view it in the console: 如果您在控制台中查看它,Chrome会为您提供一些有关keys功能只是控制台功能的小提示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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