我有这个设置器,但我不知道为什么设置值并且它不会改变:
this.setHead = function(head){
console.log('Head: x:'+this.getHead().getX()+' y:'+this.getHead().getY());
console.log('temp Head: x:'+head.getX()+' y:'+head.getY());
this.head = head;
console.log('Head: x:'+this.getHead().getX()+' y:'+this.getHead().getY());
}
Chrome日志中的结果是:
Head: x:5 y:10 // old value
temp Head: x:1 y:7 //temporary value decide to copy
Head: x:5 y:10 // and the new valụe : NO CHANGE
我已经阅读了 Javascript 通过引用传递对象,我不知道它与 Java 有什么不同。如果不是,我不知道为什么会这样。请告诉我。
谢谢 :)
@已编辑:我为日志添加了一行,并看到了奇怪的结果:
console.log('Head: x:'+this.head.getX()+' y:'+this.head.getY());
Head: x:1 y:7
这很奇怪,因为我认为它应该与下面的行相同,但事实并非如此
console.log('头:x:'+this.getHead().getX()+'y:'+this.getHead().getY());
我getHead()
的是:
this.getHead = function() {
return head;
}