Are There Pointers In Javascript?


Answer :

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


Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?