简体   繁体   English

对象中的数字键在Firefox中产生错误(“ SyntaxError:缺少;语句前”)

[英]Numeric keys in object gives error in Firefox (“SyntaxError: missing ; before statement”)

The object {'1':'test'} gives an error in Firefox, but seems to be fine in Chrome. 对象{'1':'test'}在Firefox中给出错误,但在Chrome中似乎还不错。 Does anyone know how to get around this error and make this work? 有谁知道如何解决这个错误并使之工作? The keys and values are from an external source so I can't just change them. 键和值来自外部来源,因此我不能仅仅更改它们。 (Run the code snippet below in Firefox and you will see the error.) (在Firefox中运行下面的代码段,您将看到错误。)

 {'1':'test'} 

You have to save the object in a variable or use it in any way. 您必须将对象保存在变量中或以任何方式使用。 Just writing in inside a script block does nothing. 仅仅在脚本块中写入并没有任何作用。

The following works fine: 以下工作正常:

 var obj = {'1':'test'}; alert(obj['1']); 

I tried in chrome console and firefox console. 我在chrome控制台和firefox控制台中尝试过。

Indeed, that doesn't work in Firefox, but, it's normal. 确实,这在Firefox中不起作用,但这是正常的。

In javascript you can't type JSON without variable declaration before. 在javascript中,如果您之前没有进行变量声明,则无法输入JSON。

So, Firefox doesn't understand it because javascript doesn't understand it. 因此,Firefox无法理解它,因为javascript无法理解它。

In fact, I think that Chrome overrides the javascript interpretor to allow declaration without attributions. 实际上,我认为Chrome会覆盖javascript解释器以允许声明时不带署名。

Like in python shell when you type 5, it will write 5. So with that Chrome allow you to see the structure of an array or object whatever, just by typings them without declare it into a variable. 就像在python shell中键入5一样,它将写入5。因此,通过Chrome浏览器,您无需键入任何变量即可直接查看数组或对象的结构,而无需将其声明为变量。

You can see an example here : 您可以在此处查看示例:

https://jsfiddle.net/3yqdj599/ https://jsfiddle.net/3yqdj599/

let yes = {'1':'test'};
console.log(yes)
// {'1':'test'} => that doesn't work

Finally, don't worry, since a browser is able to execute javascript, if you assign your object in a variable, it will interpret it. 最后,不用担心,由于浏览器能够执行javascript,因此,如果您在变量中分配对象,它将对其进行解释。

I hope this is clear and helped you ! 希望情况清楚,对您有所帮助! :) :)

Cya ! Cya!

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

相关问题 使用jsonp的Ajax get给出“ SyntaxError:missing; 声明前”错误 - Ajax get with jsonp gives “SyntaxError: missing ; before statement” error jQuery-Firefox开发人员工具中的错误消息:'SyntaxError:missing; 声明前” - Jquery - Error message in Firefox developer tools: 'SyntaxError: missing ; before statement' 未捕获到的SyntaxError:意外的令牌:在chrome上,“ SyntaxError:丢失; Firefox上的“声明前” - Uncaught SyntaxError: Unexpected token : on chrome and “SyntaxError: missing ; before statement” on firefox 我收到此错误SyntaxError:missing; 在Firefox语句显示错误之前 - I am getting this error SyntaxError: missing ; before statement firefox shows the error JS错误:SyntaxError:缺少; 声明前 - JS error: SyntaxError: missing ; before statement 语法错误:丢失; Wordpress中的语句前错误 - SyntaxError: missing ; before statement error in wordpress 语法错误:丢失; 声明前-没有明显的错误? - SyntaxError: missing ; before statement - no obvious error? Javascript错误:SyntaxError:缺少; 声明前 - Javascript Error: SyntaxError: missing ; before statement J查询错误:SyntaxError:丢失; 声明前 - J query Error: SyntaxError: missing ; before statement 语法错误:缺少; before 语句在 jquery 中出现此错误 - SyntaxError: missing ; before statement getting this error in jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM