为什么JS允许对象属性同时“引号”?和引用?有区别吗?

Why does JS allow object properties to be both "quoted" and non-quoted? Is there a difference?

本文关键字:引号 有区别 引用 许对象 JS 属性 对象 为什么      更新时间:2023-09-26

为什么JS允许对象属性都是"quoted"non-quoted ?有区别吗?

var object = {
    "firstName": "SpongeBob", //quoted
    lastName: "SquarePants"   //non-quoted
};
console.log(object.firstName); // -> SpongeBob
console.log(object.lastName);  // -> SquarePants

当然有:

var obj = {
    "crazy-property!name": "some string"
    // crazy-property!name is an invalid property name without quotation marks
};
console.log(obj["crazy-property!name"]);
// can't do console.log(obj.crazy-property!name)