javascript - 未捕获的 ReferenceError:未定义键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13751166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript - Uncaught ReferenceError: keys is not defined
提问by user1767962
I am getting an error when I run the following command in an included script. But if I run the command from the google chrome console, it works properly.
在包含的脚本中运行以下命令时出现错误。但是如果我从 google chrome 控制台运行命令,它就可以正常工作。
var a = {};
console.log(keys(a));
Error:
错误:
Uncaught ReferenceError: keys is not defined
What's going on here? How can I use the keys
function in an included script?
这里发生了什么?如何keys
在包含的脚本中使用该函数?
回答by Alex Wayne
console.log(keys(a))
keys()
is not function provided by the browser for use in your code. You probably want Object.keys()
keys()
不是浏览器提供的用于您的代码的功能。你可能想要Object.keys()
a = {};
console.log(Object.keys(a));
Sometimes the console has extra functions exposed to it for ease of use debugging that aren't available in your actual code. keys()
sounds like one, and copy('some text')
is another.
有时,控制台会向它公开一些额外的功能,以便于使用调试,这些功能在您的实际代码中是不可用的。keys()
听起来像一个,又copy('some text')
是另一个。
I'm failing to find a link which lists them, sadly. But I'm quite sure there are more than those 2 functions.
遗憾的是,我找不到列出它们的链接。但我很确定不止这两个功能。
回答by 0x499602D2
Whenever you get an error like this, try to search for a definition of the function/variable that's been reported as undefined. If it isdefined, try looking for a reason this might not be working. Did you know that the keys
function is apart of the Object
constructor? You can't call it as if it's a free-standing function. Though if you get into the habit of doing this, try making your own function to allow this:
每当遇到这样的错误时,请尝试搜索报告为未定义的函数/变量的定义。如果已定义,请尝试寻找可能不起作用的原因。你知道这个keys
函数是Object
构造函数的一部分吗?你不能把它当作一个独立的函数来调用。但是,如果您养成了这样做的习惯,请尝试制作自己的函数以允许这样做:
function key( object ) {
return Object.keys( object );
}
Your code should pass given a definition like this.
您的代码应该通过这样的定义。