The syntax you're currently using is Object Literal syntax, and is perfectly acceptable given what you've shown here. If you reach a point where you need to add member functions to multiple classes you'll want to read up on Prototypal Inheritance. Otherwise, you're doing just fine IMO.
var obj = {"class": 10}; // "Class" is a reserved word
alert(obj["class"]); // Shows "10"
// Using "obj.class" might give unexpected errors in some browsers
C:如果需要,始终使用引号几乎意味着它已准备好 JSON。
Assuming you mean different ways of creating Object Literals and their keys, enclosing the key with quotations would be the best way to go. There are a number of reasons:
A: If you want to give the key a name that is longer than one word, a symbol, or some other character, you'll have to use quotations.
B: Using quotations allows you to use reserved words. Without them, the key might not be accessible if the key's name is a reserved word.
var obj = {"class": 10}; // "Class" is a reserved word
alert(obj["class"]); // Shows "10"
// Using "obj.class" might give unexpected errors in some browsers
C: Always using quotations will pretty much mean it's JSON ready, if need be.
When declaring a property with this type of syntax:
var myObject = {group1 : 'sharepoint'};
you must use quotes around the property name anytime it is not a legal javascript variable name. That would include anytime it includes a space, starts with a number or includes any other character that is not legal in a javascript variable name (such as +-*&^%). It is safe to always include the quotes. Quotes are not required if the property name is safe.
Here's one explanation of what types of names would require the quotes: http://www.codelifter.com/main/tips/tip_020.shtml. If you aren't sure, put the quotes in. If you want to be consistent, always use the quotes or never use the quotes and never use identifiers that aren't legal javascript variable names. Your choice.
发布评论
评论(4)
您当前使用的语法是对象文字语法,鉴于您在此处显示的内容,这是完全可以接受的。如果您需要向多个类添加成员函数,您需要阅读原型继承。否则,在我看来,你做得很好。
The syntax you're currently using is Object Literal syntax, and is perfectly acceptable given what you've shown here. If you reach a point where you need to add member functions to multiple classes you'll want to read up on Prototypal Inheritance. Otherwise, you're doing just fine IMO.
假设您指的是创建对象文字及其键的不同方法,那么用引号将键括起来将是最好的方法。原因有很多:
A:如果您想为键指定一个比一个单词、符号或其他字符长的名称,则必须使用引号。
B:使用引号可以让您使用保留字。如果没有它们,如果密钥的名称是保留字,则可能无法访问该密钥。
C:如果需要,始终使用引号几乎意味着它已准备好 JSON。
Assuming you mean different ways of creating Object Literals and their keys, enclosing the key with quotations would be the best way to go. There are a number of reasons:
A: If you want to give the key a name that is longer than one word, a symbol, or some other character, you'll have to use quotations.
B: Using quotations allows you to use reserved words. Without them, the key might not be accessible if the key's name is a reserved word.
C: Always using quotations will pretty much mean it's JSON ready, if need be.
唯一真正的区别是,您可以通过这种方式创建无效的符号名称:
您只能通过索引器(又名
x['some text']
)访问它,但它可以工作。The only real difference is that you can make non valid symbol names that way:
You would only be able to access that via the indexer (aka
x['some text']
), but it works.当使用这种类型的语法声明属性时:
只要属性名称不是合法的 JavaScript 变量名称,您就必须在属性名称周围使用引号。这包括任何包含空格、以数字开头或包含 JavaScript 变量名称中不合法的任何其他字符(例如 +-*&^%)的情况。始终包含引号是安全的。如果属性名称是安全的,则不需要引号。
以下是对哪些类型的名称需要引号的解释:http://www.codelifter。 com/main/tips/tip_020.shtml。如果您不确定,请放入引号。如果您想保持一致,请始终使用引号,或者永远不要使用引号,并且永远不要使用不合法的 javascript 变量名称的标识符。你的选择。
When declaring a property with this type of syntax:
you must use quotes around the property name anytime it is not a legal javascript variable name. That would include anytime it includes a space, starts with a number or includes any other character that is not legal in a javascript variable name (such as +-*&^%). It is safe to always include the quotes. Quotes are not required if the property name is safe.
Here's one explanation of what types of names would require the quotes: http://www.codelifter.com/main/tips/tip_020.shtml. If you aren't sure, put the quotes in. If you want to be consistent, always use the quotes or never use the quotes and never use identifiers that aren't legal javascript variable names. Your choice.