哈希键中的 JavaScript 关键字 [英] JavaScript keywords in hash keys

查看:45
本文介绍了哈希键中的 JavaScript 关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基于 V8 的 JS 引擎中,您可以在属性键中使用不带引号的关键字,例如({ delete: 1 }),在Rhino或其他JS引擎中,会报错,怎么可能?正确的行为应该是什么?

In V8-based JS engines, you can use unquoted keywords in property keys, like ({ delete: 1 }), while in Rhino or other JS engines, it throws an error, how is that possible? What should be the correct behavior?

spaceman@spaceman-laptop:~$ rhino
Rhino 1.7 release 2 2010 01 20
js> ({ delete: 1 })        
js: "<stdin>", line 2: invalid property id
js: ({ delete: 1 })
js: .........^
js> 
spaceman@spaceman-laptop:~$ node
> ({ delete: 1})
{ delete: 1 }

推荐答案

新的 ECMAScript 5 规范 允许属性名称为保留字.一些引擎可能已经采用了这个新的特性",而另一些引擎可能仍然要求在属性名称恰好是保留字时引用.

The new ECMAScript 5 specification allows property names to be reserved words. Some engines might have adopted this new "feature", while others might still require property names to be quoted when they happen to be reserved words.

这篇关于哈希键中的 JavaScript 关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆