Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know that two javascript variable point to the same memory address

Tags:

Is there a way to know that 2 javascript variable point to the same memory address ?

var my_var = {     id: 1,     attribute: "myAttribute" }  var copy = my_var;  //someting like if(copy === my_var) return true; 
like image 496
Merlin Avatar asked Jan 30 '14 13:01

Merlin


People also ask

Does === check reference address in JavaScript?

For reference type like objects, == or === operators check its reference only. here a==b will be false as reference of both variables are different though their content are same.

How are JavaScript variables stored in memory?

“Variables in JavaScript (and most other programming languages) are stored in two places: stack and heap. A stack is usually a continuous region of memory allocating local context for each executing function. Heap is a much larger region storing everything allocated dynamically.

Which is a variable that stores the memory address of another variable?

A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory items.

How do you address a variable in JavaScript?

Use the reserved keyword var to declare a variable in JavaScript. Syntax: var <variable-name>; var <variable-name> = <value>; A variable must have a unique name.


1 Answers

You can't alias variables like you can in C. In javascript, something like

var x = 1; var y = x y = 4; // x is still 1 

will always be the case.

However, objects are always passed by reference

var x = { one: 1, two: 2 }; var y = x; y.one = 100; // x.one is now 100 
like image 119
bendecoste Avatar answered Sep 21 '22 04:09

bendecoste