javascript - 未捕获的ReferenceError:未定义键

时间:2022-11-05 15:07:25

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.

我在包含的脚本中运行以下命令时收到错误。但是,如果我从谷歌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函数?

2 个解决方案

#1


32  

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.

遗憾的是,我没有找到列出它们的链接。但我确信不仅仅有这两个功能。

#2


1  

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 is defined, 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.

您的代码应该通过给定这样的定义。

#1


32  

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.

遗憾的是,我没有找到列出它们的链接。但我确信不仅仅有这两个功能。

#2


1  

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 is defined, 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.

您的代码应该通过给定这样的定义。