0

A friend of mine told me JS has the best kind of pointers? I did a bit of research, and everywhere I looked for memory addresses in JS and pointers I found answers like

Its more or less possible It's more or less Impossible

So which is it?

How can you create a pointer in JS?

Rich Michaels
  • 1,663
  • 2
  • 12
  • 18
Ionut Eugen
  • 481
  • 2
  • 6
  • 27

2 Answers2

2

There are no pointers in the JavaScript language.

Every variable that contains an Object is actually an opaque reference to that object.

Within the interpreter that reference will take the form of a pointer, but the value of that pointer is not accessible to you.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • Hey, look, [you haven't forgotten what you knew back in 2013](https://stackoverflow.com/a/17382443/366904). – Cody Gray - on strike Jun 08 '19 at 09:57
  • Ok, but there must still be a way right ? From what I understand the browser does not give u access the the system variables and memory adresses, but how about something local like nodejs ? – Ionut Eugen Jun 08 '19 at 10:16
  • No, that would be a *massive* security violation, @Jonut. There are possibly bugs in certain implementations, but they're just that: bugs, not features. Browsers sandbox web pages so that they cannot touch the underlying system. They're clearly not going to expose features that would allow them to do so. Alnitak assumed you were talking about pointers to objects *within* the JavaScript application, which, as he said, is implicitly possible through references (since the runtime system treats those as identical to pointers, even though they aren't written as such in the source code). – Cody Gray - on strike Jun 08 '19 at 19:35
  • @CodyGray umm, oops! – Alnitak Jun 08 '19 at 21:44
0

Javascript do not support pointers. But you can make use of Object which will serve the purpose of pointers.

Refer the code below for passing parameters to function by reference :

var numbers = {num1:5,num2:8};
var mod = changeNum(numbers);

function changeNum(var Obj){
    Obj.num2 += 2;
}

Output:

{num1:5,num2:10}

Ravina D
  • 1
  • 2