Javascript variable reference / alias

Is it possible in javascript to assign an alias / link to a local var someay?

I mean something like C:

function foo() { var x = 1; var y = &x; y++; alert(x); // prints 2 } 

= EDIT =

Is it possible to use the arguments.callee alias in this code ?:

 function foo() { arguments.callee.myStaticVar = arguments.callee.myStaticVar || 0; arguments.callee.myStaticVar++; return arguments.callee.myStaticVar; } 
+88
javascript
Nov 06 '09 at 11:22
source share
7 answers

In JavaScript, primitive types, such as integers and strings, are passed by value, while objects are passed by reference. Therefore, for this you need to use an object:

 // declare an object with property x var obj = { x: 1 }; var aliasToObj = obj; aliasToObj.x ++; alert( obj.x ); // displays 2 
+171
Nov 06 '09 at 11:29
source share

To some extent this is possible, you can create an alias for the variable using closures:

 Function.prototype.toString = function() { return this(); } var x = 1; var y = function() { return x } x++; alert(y); // prints 2, no need for () because of toString redefinition 
+15
Nov 06 '09 at 12:09
source share

If you can alias, then something depends on the data type. Objects, arrays and functions will be processed using the link, and pseudonization is possible. Other types are essentially atomic, and the variable stores the value, not a reference to the value.

arguments.callee is a function, so you can reference it and modify this shared object.

 function foo() { var self = arguments.callee; self.myStaticVar = self.myStaticVar || 0; self.myStaticVar++; return self.myStaticVar; } 

Please note that if in the above code you should have said self = function() {return 42;}; , then self will refer to a different object than arguments.callee , which remains a reference to foo . When you have a compound object, the assignment operator replaces the link; it does not change the mentioned object. With atomic values, a case like y++ is equivalent to y = y + 1 , which sets the variable to "new."

+8
Nov 06 '09 at 12:11
source share

edit my previous answer: if you want to count function calls, you can try:

 var countMe = ( function() { var c = 0; return function() { c++; return c; } })(); alert(countMe()); // Alerts "1" alert(countMe()); // Alerts "2" 

Here c serves as a counter, and you do not need to use arguments.callee.

+1
Nov 06 '09 at 11:49
source share

By expanding user187291 post , you can also use getters / setters to get around to use functions.

 var x = 1; var ref = { get x() { return x; }, set x(v) { x = v; } }; (ref.x)++; console.log(x); // prints '2' x--; console.log(ref.x); // prints '1' 
+1
Aug 04 '15 at 10:54
source share

I was looking for my project today and found this that looks like the best way: http://sirdarckcat.blogspot.com/2007/07/passing-reference-to-javascript.html

0
Jul 04 '11 at 7:30
source share

In 2019, I need to write minimized jquery plugins, so I need this alias too, and therefore, testing these and other examples from other sources, I found a way without copying the entire object into memory, but creating only a link. I tested this already with Firefox and watched the memory of the task manager tab on Firefox before. The code is:

 var {p: d} ={p: document}; console.log(d.body); 
0
May 13 '19 at 10:36
source share



All Articles