+ 1
Best way to create new object?
1 Answer
+ 8
Depends on how often you need an object 'type'.
If you need the object only once its probably easier and faster like this:
var person = {
name:'John',
age:'20'
}
If you need the same object type more than once its a good idea to use the object constructor. It keeps your code organized and you dont need to remember how every object has been defined before (property names).
Do it like this:
function person(name, age) {
this.name = name,
this.age = age
}
and then use it like this
var aPerson = person(John, 20);