Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript equivalent of assign by reference?

Is there any way I can be less verbose in JavaScript by pointing a local variable by to an objects property?

For instance in PHP I can do this:

$obj->subobject->property = 'Foo';
$property =& $obj->subobject->property;
$property =  'Bar';
echo $obj->subobject->property;
// output 'Bar'

It's not a very good example but you get the idea.

I want to copy this behaviour in Javascript. I'm quite often having to go quite deep into objects and it's getting quite annoying having to do:

if (please.stop.making.me[somevar].type.so.much.length) {
    please.stop.making.me[somevar].type.so.much[newSubObjectKey] = anObject;
}

// perform more operations on the object down here

It would be a lot easier to read and a lot easier to type:

var subObj = is.much.easier.to.type.once;
if (subObj.length) {
     subObj[newSubObjectKey] = anObject;
}

// now that's much better

I know I should really know this already, but I'm just advancing to "advanced novice" in JavaScript.

like image 879
rich97 Avatar asked Jul 18 '11 12:07

rich97


People also ask

Is JavaScript assign by reference?

The Bottom Line on JavaScript ReferencesOn variable assignment, the scalar primitive values (Number, String, Boolean, undefined, null, Symbol) are assigned-by-value and compound values are assigned-by-reference. The references in JavaScript only point at contained values and NOT at other variables, or references.

Is JavaScript by reference or by value?

In JavaScript, all function arguments are always passed by value. It means that JavaScript copies the values of the variables into the function arguments. Any changes that you make to the arguments inside the function do not reflect the passing variables outside of the function.

Is JavaScript is pass by value or pass by reference?

JavaScript is always pass-by-value. This means everything in JavaScript is a value type and function arguments are always passed by value.

How do you create a reference variable in JavaScript?

In JavaScript, it's just NOT possible to have a reference from one variable to another variable. And, only compound values (Object, Array) can be assigned by reference. Bottom line: The typeof value assigned to a variable decides whether the value is stored with assign-by-value or assign-by-reference.


2 Answers

In JavaScript, everything is passed by value, but the variable's type will determine whether it's a reference passed by value or not;

  • Objects are references
  • Primitives (numbers, strings etc) are passed by value.

In simple terms, if you pass a variable to a function that's an array, modifying it in the function will affect the parent.

However, passing it a value in the array will not. Naturally, there's absolutely nothing stopping you wrapping a primitive in an object to ensure it works like a "pointer".

like image 122
Olipro Avatar answered Oct 01 '22 23:10

Olipro


You can assign a new variable to reference any depth in a chain of property keys, so long as the entry referred to isn't a primitive type.

This works because a bare object variable is actually a reference to that variable, so your new (shorter) variable can point to the same place.

However primitive number and string values are passed by value, so you can't create new references to those.

like image 39
Alnitak Avatar answered Oct 01 '22 23:10

Alnitak