引用和未引用的JavaScript对象属性之间的区别 [英] Difference between quoted and un-quoted JavaScript object properties

查看:48
本文介绍了引用和未引用的JavaScript对象属性之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

引用和未引用的JavaScript对象属性/方法名称之间是否有区别?

Is there a difference between quoted and un-quoted JavaScript object property/method names?

例如,这两者之间有什么区别:

For example, what is the difference between these two:

var obj1 = {
  property1 : "Value 1",
  method1 : function() {
    return true;
  }
};

var obj2 = {
  "property1" : "Value 1",
  "method1" : function() {
    return true;
  }
};


推荐答案

JavaScript没有区别。但是,您必须引用恰好是保留字的属性名称(例如 class ),或者包含无效字符的名称(例如 first-姓名)。

There is no difference in JavaScript. However you will have to quote property names that happen to be reserved words (such as class), or names that contain invalid characters (such as first-name).

这篇关于引用和未引用的JavaScript对象属性之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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