传递给另一个对象的对象的更改是否也在第一个对象中发生?

时间:2016-07-15 10:23:04

标签: javascript oop

[Compare(Constants.NewPassword, ErrorMessage = businessprovider.GetValueByKey(Constants.ErrorMsg))]

我能以这种方式得到变量(4)“数字”的值:function Game() { this.player = new Component(); this.renderer = new Renderer(this.player); } function Renderer(player) { this.player = player; this.player.number = 4; } var game = new Game();

1 个答案:

答案 0 :(得分:0)

回答你的问题,是的。您可以编辑对象的内容并将其反映在原始对象中,但如果您尝试覆盖整个对象(它的引用),则不能。

您可以在此jsfiddle中查看示例(选择Run left left): https://jsfiddle.net/kcx61hye/34/

var player = {number:1};
var player2 = {number:2};
function Renderer(player) {
    this.player = player;
    this.player.number = 4;
}
function Renderer2(player) {
    this.player=player2;
}
document.write(player.number); //Outputs 1
Renderer(player);
document.write(player.number); //Outputs 4, so object is modified
Renderer(player2);
document.write(player.number); //outputs 4, so no modification