Javascript 变量作为对象指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17872317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:55:41  来源:igfitidea点击:

Javascript Variables as Object Pointers

javascriptpass-by-reference

提问by nemo

I have a question out of curiosity. So I looked into how JS handles variable assignment and I get it. How does variable assignment work in JavaScript?

出于好奇,我有一个问题。所以我研究了 JS 如何处理变量赋值,我明白了。JavaScript 中的变量赋值是如何工作的?

But the same principle doesn't seem to exhibit itself in the following code I am working on:

但同样的原则似乎并没有在我正在处理的以下代码中体现出来:

var temp = playlist1[0];
playlist1[0] = playlist1[1];
playlist1[1] = temp;

I know this is a standard way to swap array elements. But if temp is pointing at playlist1[0], and playlist1[0]'s contents are changed to playlist1[1]'s then how come I don't end up with two playlist1[1]values in a row?

我知道这是交换数组元素的标准方法。但是如果 temp 指向playlist1[0],并且playlist1[0]'s 的内容更改为playlist1[1]'s 那么为什么我不会playlist1[1]连续得到两个值?

回答by newacct

Not only variablesare object pointers. All values(that are not primitives) are object pointers. So tempis an object pointer. playlist1is a object pointer to an array object whose elements are object pointers. e.g. playlist1[0]is an object pointer, playlist1[1]is an object pointer, etc.

不仅变量是对象指针。所有值(不是基元)都是对象指针。temp对象指针也是如此。playlist1是指向数组对象的对象指针,其元素是对象指针。例如playlist1[0]是一个对象指针,playlist1[1]是一个对象指针,等等。

But if temp is pointing at playlist1[0]

但是如果 temp 指向 playlist1[0]

This doesn't make sense. tempis an object pointer. It points to an object. playlist1[0]is not an object; it's an object pointer. temp = playlist1[0];makes the object pointer temppoint to the same object as object pointer playlist1[0].

这没有意义。temp是一个对象指针。它指向一个对象。playlist1[0]不是一个对象;它是一个对象指针。temp = playlist1[0];使对象指针temp指向与对象指针相同的对象playlist1[0]

If you know C, it is equivalent to something like this:

如果你知道 C,它就相当于这样:

Object *playlist1[10];

Object *temp = playlist1[0];
playlist1[0] = playlist1[1];
playlist1[1] = temp;

回答by TGH

This is consistent with the answer in the referenced question: You are just changing which object the variable points to - not the data it used to point to. Meaning temp is unaffected by the move to have playlist1[1] point to playlist1[2]. Temp retains the original value it pointed to when playlis1[1] and temp both pointed to it. Only playlist1[1] is updated

这与引用问题中的答案一致:您只是在更改变量指向的对象 - 而不是它曾经指向的数据。这意味着 temp 不受让 playlist1[1] 指向 playlist1[2] 的移动的影响。当 playlis1[1] 和 temp 都指向它时,Temp 保留它所指向的原始值。仅更新播放列表 1[1]

回答by mckurt

Because those are still references to elements in the array and not the elements themselves. So in the line:

因为这些仍然是对数组中元素的引用,而不是元素本身。所以在这一行:

playlist[1]=playlist[2]

You are not changing anything about temp. Contrast that with something like (assuming array elements were objects):

你没有改变任何关于温度的东西。对比一下(假设数组元素是对象):

playlist[1].x=playlist[2].x

That is actually assigning the value of the object in the array, and if temp pointed to playlist[1], then temp.x would equal playlist[2].x

那实际上是在数组中分配对象的值,如果 temp 指向 playlist[1],那么 temp.x 将等于 playlist[2].x

回答by bortunac

say we have obj={l1:{l2:[1,2]},}and we want to address obj.l1.l2[1]using an array of levels like arr=["l1","l2",1]then :

假设我们有 obj={l1:{l2:[1,2]},}并且我们想obj.l1.l2[1]使用像arr=["l1","l2",1]then这样的一组级别来解决:

Object.defineProperty(Object.prototype,'point',{
    value:function(arr){
        var rez=this;
        for(var s in arr){
            rez=rez[arr[s]];
            if(rez === undefined) return undefined;
        }
        return rez;
    }
});

So after defining "point" method (which is not enumerable to mess up everithing) we can use

因此,在定义“点”方法(不可枚举以弄乱一切)之后,我们可以使用

obj.point(arr)

to get value 2

获得价值 2