【发布时间】:2011-04-14 18:26:23
【问题描述】:
我想设置一个变量以指向新创建的对象中的属性以保存“查找”,如下例所示。基本上,我认为变量是对对象属性的引用。不是这种情况;看起来变量包含该值。第一个 console.log 是 1(这是我要分配给 photoGalleryMod.slide 的值),但是当查看 photoGalleryMod.slide 时,它仍然是 0。
有没有办法做到这一点?谢谢。
(function() {
var instance;
PhotoGalleryModule = function PhotoGalleryModule() {
if (instance) {
return instance;
}
instance = this;
/* Properties */
this.slide = 0;
};
}());
window.photoGalleryMod = new PhotoGalleryModule();
/* Tried to set a variable so I could use test, instead of writing photoGalleryMod.slide all the time plus it saves a lookup */
var test = photoGalleryMod.slide;
test = test + 1;
console.log(test);
console.log(photoGalleryMod.slide);
【问题讨论】:
标签: javascript object reference