了解创建 JavaScript 对象的不同方法

发布于 2024-11-19 15:48:45 字数 1433 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你在我安 2024-11-26 15:48:45

您当前使用的语法是对象文字语法,鉴于您在此处显示的内容,这是完全可以接受的。如果您需要向多个类添加成员函数,您需要阅读原型继承。否则,在我看来,你做得很好。

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.

往昔成烟 2024-11-26 15:48:45

假设您指的是创建对象文字及其键的不同方法,那么用引号将键括起来将是最好的方法。原因有很多:

A:如果您想为键指定一个比一个单词、符号或其他字符长的名称,则必须使用引号。

var obj = {"some name": 1337, "¤": "š"}; 
alert(obj["some name")); // Shows "1337"
alert(obj["¤"]); // Shows "š"

B:使用引号可以让您使用保留字。如果没有它们,如果密钥的名称是保留字,则可能无法访问该密钥。

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.

var obj = {"some name": 1337, "¤": "š"}; 
alert(obj["some name")); // Shows "1337"
alert(obj["¤"]); // Shows "š"

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.

情释 2024-11-26 15:48:45

唯一真正的区别是,您可以通过这种方式创建无效的符号名称:

var x = { 'some text': { prop1: true } }; // Valid
var x = { some text: { prop1: true } }; // Invalid

您只能通过索引器(又名 x['some text'])访问它,但它可以工作。

The only real difference is that you can make non valid symbol names that way:

var x = { 'some text': { prop1: true } }; // Valid
var x = { some text: { prop1: true } }; // Invalid

You would only be able to access that via the indexer (aka x['some text']), but it works.

屋顶上的小猫咪 2024-11-26 15:48:45

当使用这种类型的语法声明属性时:

var myObject = {group1 : 'sharepoint'};

只要属性名称不是合法的 JavaScript 变量名称,您就必须在属性名称周围使用引号。这包括任何包含空格、以数字开头或包含 JavaScript 变量名称中不合法的任何其他字符(例如 +-*&^%)的情况。始终包含引号是安全的。如果属性名称是安全的,则不需要引号。

以下是对哪些类型的名称需要引号的解释:http://www.codelifter。 com/main/tips/tip_020.shtml。如果您不确定,请放入引号。如果您想保持一致,请始终使用引号,或者永远不要使用引号,并且永远不要使用不合法的 javascript 变量名称的标识符。你的选择。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文