Javascript 中有指针吗?

我以前用过 C + + ,我意识到指针是非常有用的。Javascript 中有什么东西像指针一样起作用吗?Javascript 有指针吗?我喜欢使用指针,当我想使用这样的东西:

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

我知道这是一个非常简单的示例,我可以只使用 a,但是在一些更复杂的示例中,我会发现指针非常有用。有什么想法吗?

115293 次浏览

No, JS doesn't have pointers.

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.

Within a function, one may change the contents of 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

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

//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.)

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

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

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.

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:

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

I just did a bizarre thing that works out, too.

Instead of passing a pointer, pass a function that fills its argument into the target variable.

var myTarget;


class dial{
constructor(target){
this.target = target;
this.target(99);
}
}
var myDial = new dial((v)=>{myTarget = v;});

This may look a little wicked, but works just fine. In this example I created a generic dial, which can be assigned any target in form of this little function "(v)=>{target = v}". No idea how well it would do in terms of performance, but it acts beautifully.

Assigning by reference and arrays.

let pizza = [4,4,4];
let kebab = pizza;  // both variables are references to shared value
kebab.push(4);
console.log(kebab); //[4,4,4,4]
console.log(pizza); //[4,4,4,4]

Since original value isn't modified no new reference is created.

kebab = [6,6,6,6];  // value is reassigned
console.log(kebab); //[6,6,6,6]
console.log(pizza); //[4,4,4,4]

When the compound value in a variable is reassigned, a new reference is created.