为什么JS允许对象属性既“引用”又不引用?有区别吗?

时间:2014-07-18 21:39:00

标签: javascript

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

var object = {

    "firstName": "SpongeBob", //quoted
    lastName: "SquarePants"   //non-quoted

};

console.log(object.firstName); // -> SpongeBob
console.log(object.lastName);  // -> SquarePants

1 个答案:

答案 0 :(得分:0)

当然有:

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)