请阅读下面代码中的注释以了解我想问什么。
预期输出:根据 JavaScript 中的按引用传递机制,objOne
预期会{}
在最后记录日志,因为objTwo
已使用{}
.
var objOne = {
x: 1,
y: 2
};
var objTwo = objOne;
// change the x vlaue to 2 by objTwo
objTwo.x = 2;
// Change the value of key x in objOne as well - pass by reference mechanism
console.log(objOne); // { x: 2, y: 2 }
/*** Pass by reference is understood in code, above this comment ***/
// Now what if objTwo initialized with empty object
objTwo = {};
console.log(objOne); // { x: 2, y: 2 } but expected output = {}
// As per pass by reference mechanism. objOne is expected to log {}, because objTwo was initialized with {}.