Difference between object definition and variable definition

Hi guys!

I am wondering what is the difference between object definition and variable definition but using in this way:

var object = {};

object[myQuery] = this.prop.myQuery;
object[destination] = object.myQuery.fullDestination

and

var myQuery = this.prop.myQuery;
var destination = myQuery.fullDestination;

I was thinking something about memory leaks but i don’t know the answer :frowning:

Thank you for your comments in advance.

This first:

var object = {};

object[myQuery] = this.prop.myQuery;

Is saying “assign whatever this.prop.myQuery is to a property I’m creating on object that has a key of whatever string myQuery is”.

Bit by bit:

  • the variable object has been assigned an empty object.
  • to assign properties to objects, you do object.myProperty = "foo" or object['myProperty'] = "foo".
  • object would then look like {myProperty: "foo"}.
  • In this case, the property name is a variable, so myQuery has to be a string.

This next, and I think there is an error here:

object[destination] = object.myQuery.fullDestination

Another property is being assigned, so I assume what myQuery represents is another object. You are taking a property in that other object (fullDestination) and copying it to the top level, so you end up with:

{
  myQuery: { /* some query stuff here */ },
  destination: "the destination"
}

The reason I think there are errors is that afaics the second and third lines should be

object.myQuery = this.prop.myQuery;
object.destination = object.myQuery.fullDestination

Or

object['myQuery'] = this.prop.myQuery;
object['destination'] = object.myQuery.fullDestination

Then this:

var myQuery = this.prop.myQuery;
var destination = myQuery.fullDestination

You’re just assigning to variables rather than putting the values in an object.

It’s got nothing to do with memory leaks or anything, the first one you’re storing values in an object, the second you’re storing values directly in variables.

1 Like

Thank you for take time and give in more details that kind of difference :slight_smile: