函数参数和对象

时间:2018-08-17 18:24:04

标签: javascript function parameter-passing pass-by-reference

function f(o) {
  o.message = "set in f";
  o = {
    message: "new object!"
  };
  console.log(`inside f: o.message="${o.message}" (after assignment)`);
}
let o = {
  message: 'initial value'
};
console.log(`before calling f: o.message="${o.message}"`);
f(o);
console.log(`after calling f: o.message="${o.message}"`);

如果运行此示例,您将看到:

before calling f: o.message="initial value"
inside f: o.message="new object!" (after assignment)
after calling f: o.message="set in f"

我正在阅读学习javascript的书,而且我不理解这段代码,尤其是console.log中的代码确实令人困惑。

谢谢。

0 个答案:

没有答案