Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create a pointer to an object property in javascript?

Tags:

People also ask

How do you declare a pointer in JavaScript?

Create a pointer variable with the name ptr , that points to an int variable ( myAge ). Note that the type of the pointer has to match the type of the variable you're working with. Use the & operator to store the memory address of the myAge variable, and assign it to the pointer.

Can you add a property to an object in JavaScript?

The name: values pairs in JavaScript objects are called properties. We can add the property to JavaScript object using a variable as the name by using dot notation or bracket notation.

Can you use pointers in JavaScript?

TL;DR: There are NO pointers in JavaScript and references work differently from what we would normally see in most other popular programming languages. 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.

What are object properties in JavaScript?

An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method. In addition to objects that are predefined in the browser, you can define your own objects.


If I have an object like this:

obj = {a:{aa:1}, b:2};

and I want to create a shortcut variable (pointer to obj.a.aa) x like this:

x = obj.a.aa;

and then I want to assign the value 3 to obj.a.aa using x like this:

x = 3;  // I would like for obj.a.aa to now equal 3
console.log(obj.a.aa); // 1  (I want 3)

How can I set x up to result in the value 3 going into obj.a.aa?

I understand that obj.a.aa is a primitive, but how can I define a variable that points to it which I can then use to assign a value to the property?