var o = {a:1}; var b = Object(o); 和 var b = o 有什么不同?



 var o = {a:1};

有个如上对象


 var b = Object(o);


 var b = o

有什么不同?

JavaScript object

phoetry 10 years ago

@910JQK 说的就对了,那句话来源 Object - JavaScript|MDN

可以做个实验……


 var o = {a:1}; 
var b = Object(o);

console.log(o === b);     // true

o.a = 13;
console.log(b.a);         // 13

ahiru answered 10 years ago

结果上没有什么不同。
"If the value is an object already, it will return the value." (MDN)

lsjsf answered 10 years ago

负责任的告诉你 没有任何不同

asahui answered 10 years ago

问题的关键在于为何要多此一举地用Object()来处理对象直接量o;

假如o不是对象直接量;


 var o = "xxx";
console.log(Object(o));//String {0: "x", 1: "x", 2: "x", length: 3, [[PrimitiveValue]]: "xxx"}
var o = 1;
console.log(Object(o));//Number {[[PrimitiveValue]]: 1}

也许,只是为了强制把基本数据类型的变量转换为引用类型,然后传递给b;

喵了个咪的吖 answered 10 years ago

Your Answer