关于Javascript指向对象属性的指针的问题

发布于 2024-11-01 18:08:16 字数 743 浏览 1 评论 0原文

我想设置一个变量来指向新创建的对象中的属性以保存“查找”,如下例所示。基本上,我认为变量是对对象属性的引用。事实并非如此;看起来变量保存了值。第一个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);

I wanted to set a variable to point to property in an newly created object to save a "lookup" as shown in the example below. Basically, I thought the variable is a reference to the object's property. This is not the case; it looks like the variable holds the value. The first console.log is 1 (which is the value I want to assign to photoGalleryMod.slide) but when looking at photoGalleryMod.slide, it's still 0.

Is there a way to do this? Thanks.

(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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱*していゐ 2024-11-08 18:08:16

是的,您正在复制该值,因为“幻灯片”被设置为原始类型。试试这个:

this.slide = [0];

然后

var test = photoGalleryMod.slide;
test[0] = test[0] + 1;

更改日志记录:

console.log(test[0]);
console.log(photoGalleryMod.slide[0]);

在这种情况下,您将看到您确实拥有对同一对象的引用。与其他一些语言 (C++) 不同,JavaScript 中无法说“请给我这个变量的别名”。

Yes, you're making a copy of the value, because "slide" is set to a primitive type. Try this:

this.slide = [0];

and then

var test = photoGalleryMod.slide;
test[0] = test[0] + 1;

then change the logging:

console.log(test[0]);
console.log(photoGalleryMod.slide[0]);

In that case you'll see that you do have a reference to the same object. Unlike some other languages (C++) there's no way to say, "Please give me an alias for this variable" in JavaScript.

淡淡绿茶香 2024-11-08 18:08:16

看起来变量保存了值

是正确的。由于您使用的是数字基元,因此变量包含值而不是指向它。变量仅在引用对象时才包含引用。

实现方法是使用对象属性并指向该对象 - 这正是 photoGalleryMod 及其 slide 属性所具有的功能。

it looks like the variable holds the value

That's correct. Since you're using number primitives, variables contain the value rather than pointing to it. Variables only contain references when they're referring to objects.

The way to do it is to use an object property and to point to the object — which is exactly what you have with photoGalleryMod and its slide property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文