javascript javascript中有指针吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17382427/
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 08:07:27  来源:igfitidea点击:

Are there pointers in javascript?

javascriptpointers

提问by Dylan Vander Berg

I used C++ before and I realized that pointers were very helpful. Is there anything in javascript that acts like a pointer? Does javascript have pointers? I like to use pointers when I want to use something like:

我之前使用过 C++,我意识到指针非常有用。javascript 中是否有任何类似指针的东西?javascript 有指针吗?当我想使用以下内容时,我喜欢使用指针:

var a = 1;
var b = "a";
document.getElementById(/* value pointed by b */).innerHTML="Pointers";

I know that this is an extremely simple example and I could just use a, but there are several more complex examples where I would find pointers very useful. Any ideas?

我知道这是一个非常简单的例子,我可以只使用a,但有几个更复杂的例子,我会发现指针非常有用。有任何想法吗?

回答by Alnitak

No, JS doesn't have pointers.

不,JS 没有指针。

Objects are passed around by passing a copy of a reference. The programmer cannot access any C-like "value" representing the address of an object.

通过传递引用副本来传递对象。程序员不能访问任何代表对象地址的类似 C 的“值”。

Within a function, one may change the contentsof a passed object via that reference, but you cannot modify the reference that the caller had because your reference is only a copy:

在函数中,可以通过该引用更改传递对象的内容,但您不能修改调用者拥有的引用,因为您的引用只是一个副本:

var foo = {'bar': 1};

function tryToMungeReference(obj) {
    obj = {'bar': 2};  // won't change caller's object
}

function mungeContents(obj) {
    obj.bar = 2;       // changes _contents_ of caller's object
}

tryToMungeReference(foo);
foo.bar === 1;   // true - foo still references original object

mungeContents(foo);
foo.bar === 2;  // true - object referenced by foo has been modified

回答by user3015682

You bet there are pointers in JavaScript; objects are pointers.

你打赌 JavaScript 中有指针;对象是指针。

//this will make object1 point to the memory location that object2 is pointing at
object1 = object2;

//this will make object2 point to the memory location that object1 is pointing at 
function myfunc(object2){}
myfunc(object1);

If a memory location is no longer pointed at, the data there will be lost.

如果不再指向内存位置,则那里的数据将丢失。

Unlike in C, you can't see the actual address of the pointer nor the actual value of the pointer, you can only dereference it (get the value at the address it points to.)

与在 C 中不同,您看不到指针的实际地址,也看不到指针的实际值,您只能取消引用它(在它指向的地址处获取值。)

回答by Toshiro99

due to the nature of JS that passes objects by value (if referenced object is changed completely) or by reference (if field of the referenced object is changed) it is not possible to completely replace a referenced object.

由于 JS 通过值(如果引用的对象完全更改)或通过引用(如果引用对象的字段更改)传递对象的性质,不可能完全替换引用的对象。

However, let's use what is available: replacing single fields of referenced objects. By doing that, the following function allows to achieve what you are asking for:

但是,让我们使用可用的方法:替换引用对象的单个字段。通过这样做,以下功能可以实现您的要求:

function replaceReferencedObj(refObj, newObj) {
    let keysR = Object.keys(refObj);
    let keysN = Object.keys(newObj);
    for (let i = 0; i < keysR.length; i++) {
        delete refObj[keysR[i]];
    }
    for (let i = 0; i < keysN.length; i++) {
        refObj[keysN[i]] = newObj[keysN[i]];
    }
}

For the example given by user3015682 you would use this function as following:

对于 user3015682 给出的示例,您将使用此函数如下:

replaceReferencedObj(foo, {'bar': 2})

回答by ailton.b.s.j

Try this:

试试这个:

//MaybePointer Lib
function MaybePointer(v){
    this.val = v;
}
MaybePointer.prototype.valueOf = function(){ return this.val };

//Your code
var a = new MaybePointer(1);
var b = a;
document.getElementById(b+'').innerHTML="Pointers 1";

var c = new MaybePointer(2);
var b = c;
document.getElementById(b+'').innerHTML="Pointers 2";
<div id="1"></div>
<div id="2"></div>

回答by Sam Araiza

Technically JS doesn't have pointers, but I discovered a way to imitate their behavior ;)

从技术上讲,JS 没有指针,但我发现了一种模仿它们行为的方法;)

var car = {
    make: 'Tesla',
    nav: {
       lat: undefined,
       lng: undefined
    }
};

var coords: {
    center: {
       get lat() { return car.nav.lat; }, // pointer LOL
       get lng() { return car.nav.lng; }  // pointer LOL
    }   
};

car.nav.lat = 555;
car.nav.lng = 777;

console.log('*** coords: ', coords.center.lat); // 555
console.log('*** coords: ', coords.center.lng); // 777