给定一个对象字面量或 jQuery(html, attributes) 对象,是否有任何规范规定必须引用保留字或将来的保留字?
或者,例如,可以将 class 设置为对象的属性名称,而无需使用引号将属性名称括起来,这种做法不违反有关标识符、属性名称的规范,还是使用保留字?
寻求关于此问题的结论性答案以避免混淆。
let objLit = {
class: 123,
var: "abc",
let: 456,
const: "def",
import: 789
}
console.dir(objLit);
jQuery("<div>012</div>", {
class: "ghi"
})
.appendTo("body");<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
相关:
规范
Identifier Names are tokens that are interpreted according to the grammar given in the “Identifiers” section of chapter 5 of the Unicode standard, with some small modifications.
An Identifier is an IdentifierName that is not a ReservedWord
最佳答案
ECMAScript 5+
不,自 ECMAScript 5 以来不需要引号。原因如下:
正如您在帖子中提到的,来自 ECMAScript® 5.1 Language Specification :
7.6 Identifier Names and Identifiers
Identifier Names are tokens that are interpreted according to the grammar given in the “Identifiers” section of chapter 5 of the Unicode standard, with some small modifications. An
Identifieris anIdentifierNamethat is not aReservedWord(see 7.6.1).[...]
Syntax
Identifier :: IdentifierName but not ReservedWord
根据规范,ReservedWord 是:
7.6.1 Reserved Words
A reserved word is an
IdentifierNamethat cannot be used as anIdentifier.Syntax
ReservedWord :: Keyword FutureReservedWord NullLiteral BooleanLiteral
这包括关键字、 future 关键字、null 和 bool 文字。完整名单如下:
7.6.1.1 Keywords
break do instanceof typeof case else new var catch finally return void continue for switch while debugger function this with default if throw delete in try7.6.1.2 Future Reserved Words
class enum extends super const export import7.8.1 Null Literals
null7.8.2 Boolean Literals
true false
以上(第 7.6 节)暗示 IdentifierName 可以是 ReservedWord,并且来自 object initializers 的规范:
11.1.5 Object Initialiser
[...]
Syntax
ObjectLiteral : { } { PropertyNameAndValueList } { PropertyNameAndValueList , }
根据规范,PropertyName 是:
PropertyName : IdentifierName StringLiteral NumericLiteral
如您所见,PropertyName 可能是 IdentifierName,因此 ReservedWord 可以是 PropertyName秒。这最终告诉我们,根据规范,允许使用 ReservedWord,例如 class 和 var 作为PropertyName 不带引号,就像字符串文字或数字文字一样。
ECMAScript <>
要更深入地了解为什么在 ES5 之前的早期版本中不允许这样做,您必须查看 PropertyName 是如何定义的。根据 ECMAScript® 3 Language Specification :
PropertyName : Identifier StringLiteral NumericLiteral
如您所见,PropertyName 是一个 Identifer - 而不是 IdentifierName,因此导致无法使用 ReservedWords 作为 PropertyNames。
关于javascript - javascript对象的属性名设置保留字需要加引号吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40209367/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我正在使用ruby1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信