You can do it both ways. I prefer creating the empty object using {} and then adding the needed props but you can make it by defining the props with the initialization of the value:
var car = {};
or
var car = {
color: null,
seating: null,
fuelconsumption: null
};
Just like you did. I dont think there is a best practise for doing this. But maybe the values shoud point the needed type of the data saved this property.
Example:
var car = {
color:"",
seating: "",
fuelconsumption: ""
};
In you case "" is fine.
If using number NaN, undefined.
If using strings "".
If using objects or arrays {} [].
If using some kind of boolen values true/false
You can do it both ways. I prefer creating the empty object using {} and then adding the needed props but you can make it by defining the props with the initialization of the value:
var car = {};
or
var car = {
color: null,
seating: null,
fuelconsumption: null
};
Just like you did. I dont think there is a best practise for doing this. But maybe the values shoud point the needed type of the data saved this property.
Example:
var car = {
color:"",
seating: "",
fuelconsumption: ""
};
In you case "" is fine.
If using number NaN, undefined.
If using strings "".
If using objects or arrays {} [].
If using some kind of boolen values true/false
In Javascript, there is often not a need to initialize a property to null. This is because referencing a property that has not been initialized just harmlessly returns undefined which can work just as well as null. This is obviously different than some other languages (Java, C++) where properties of an object must be declared before you can reference them in any way.
What I would think might make sense in your case is to create a constructor function for creating a car object because you will presumably be creating more than one car and all possible car objects won't be known at the time you write the code. So, you create a constructor function like this:
function Car(color, seating, fuel) {
this.color = color;
this.seating = seating;
this.fuelConsumption = fuel;
}
Then, you can create a new Car object like this:
var c = new Car('black', 'leather', 'moderate');
console.log(c.color); // 'black'
Objects
There is no benefit to using new Object(), whereas {} can make your code more compact, and more readable.
For defining empty objects they're technically the same. The {} syntax is shorter, neater (less Java-ish), and allows you to instantly populate the object inline - like so:
var myObject = {
title: 'Frog',
url: '/img/picture.jpg',
width: 300,
height: 200
};
Arrays
For arrays, there's similarly almost no benefit to ever using new Array() over [] — with one minor exception:
var emptyArray = new Array(100);
creates a 100 item long array with all slots containing undefined, which may be nice/useful in certain situations (such as (new Array(9)).join('Na-Na ') + 'Batman!').
My recommendation
- Never use
new Object();— it's clunkier than{}and looks silly. - Always use
[]— except when you need to quickly create an "empty" array with a predefined length.
Yes, There is a difference, they're not the same. It's true that you'll get the same results but the engine works in a different way for both of them. One of them is an object literal, and the other one is a constructor, two different ways of creating an object in javascript.
var objectA = {} //This is an object literal
var objectB = new Object() //This is the object constructor
In JS everything is an object, but you should be aware about the following thing with new Object(): It can receive a parameter, and depending on that parameter, it will create a string, a number, or just an empty object.
For example: new Object(1), will return a Number. new Object("hello") will return a string, it means that the object constructor can delegate -depending on the parameter- the object creation to other constructors like string, number, etc... It's highly important to keep this in mind when you're managing dynamic data to create objects..
Many authors recommend not to use the object constructor when you can use a certain literal notation instead, where you will be sure that what you're creating is what you're expecting to have in your code.
I suggest you to do a further reading on the differences between literal notation and constructors on javascript to find more details.
I have following scenario. I have an array of objects. I loop through the array and based on value of one object i want to initialise an object. Whats the best approach to do this.
Note: I initialised the object with empty string and it works but I wanted to know if there is any better approach.
Wouldn't that work?
Core.registry.taskItemSelected = {
id: null,
name: null,
parent: null,
...
};
Something like this should work:
var props = ["id", "name", "parent", ...];
Core.registry.taskItemSelected = {};
for (var i = 0; i < props.length; i++)
Core.registry.taskItemSelected[props[i]] = "";
Edit: following the OP comments, here is better version with same final result:
Object.prototype.declare = function (varArray) {
for (var i = 0; i < varArray.length; i++) {
this[varArray[i]] = {};
}
};
//usage:
var props = ["id", "name", "parent"];
Core = {};
Core.declare(props);
And live test case as well: http://jsfiddle.net/5fRDc/
I have an array of objects. Id of all objects is unique. I am looping through the array and based on id assigning one object from an array to an empty object. So how can i initialise the object its assignment in loop ?
Note: I have initialised it with an string and it works, but since its an object not a string, I was wondering what would be the best approach?