Object constructor question


#1

Is there a difference between this...

var me=new Object();
me.name="kane";
me["age"]=22;

as can be seen here- https://www.codecademy.com/courses/javascript-beginner-en-9Sgpi/2/3?curriculum_id=506324b3a7dffd00020bf661

and

var me={};
me.name="kane";
me["age"]=22;

used here with var friends- https://www.codecademy.com/courses/javascript-beginner-en-3bmfN/0/1?curriculum_id=506324b3a7dffd00020bf661

and here-
https://www.codecademy.com/courses/javascript-beginner-en-9Sgpi/2/1?curriculum_id=506324b3a7dffd00020bf661


#2

Apart from some obscure implementation, new Object() is rarely used. The constructor takes only one argument, an object; or no argument, as it were.

var obj1 = new Object();

console.log(obj1.constructor); // [Function: Object]

var obj2 = {};

console.log(obj2.constructor); // [Function: Object]

Both statements initialize an empty object. The literal has the advantage that we can declare properties in place, if we wish. It's also the way we see many objects declared. One expects readily adopted by authors.

In JS, bracket notation, object['property'] is not the norm. Dot notation is. object.property. When declaring objects in literal terms, the shortest approach is the best,

var me = {
    name: "Kane",
    age: 22
};

The less literal approach would suggest we have some unknown data that is handed to us which we plan to store as an object:

var someName = "Kane";
var someAge = 22;

var me = {
    name: someName,
    age: someAge
};

or,

var me = {};
me.name = someName;
me.age = someAge;

#3

@mtf
thank you. that clears the doubt.