Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating New Objects in JavaScript

I'm a relatively newbie to object oriented programming in JavaScript, and I'm unsure of the "best" way to define and use objects in JavaScript. I've seen the "canonical" way to define objects and instantiate a new instance, as shown below.

function myObjectType(property1, propterty2) {
    this.property1 = property1,
    this.property2 = property2
}
// now create a new instance
var myNewvariable = new myObjectType('value for property1', 'value for property2');

But I've seen other ways to create new instances of objects in this manner:

var anotherVariable = new someObjectType({
    property1:    "Some value for this named property",
    property2:    "This is the value for property 2"
});

I like how that second way appears - the code is self documenting. But my questions are:

  1. Which way is "better"?

  2. Can I use that second way to instantiate a variable of an object type that has been defined using the "classical"way of defining the object type with that implicit constructor?

  3. If I want to create an array of these objects, are there any other considerations?

Thanks in advance.

like image 311
Ken Ray Avatar asked Apr 16 '10 13:04

Ken Ray


People also ask

How do you create a new object in JavaScript?

Creating a JavaScript Object Create a single object, using an object literal. Create a single object, with the keyword new . Define an object constructor, and then create objects of the constructed type. Create an object using Object.create() .

Can we create set of objects in JavaScript?

You can create a JavaScript Set by: Passing an Array to new Set() Create a new Set and use add() to add values. Create a new Set and use add() to add variables.

How many ways we can create object in JavaScript?

You can create an object in three different ways: Using object literal. By creating instance of Object directly. By using constructor function.


1 Answers

It's really down to taste. This way:

var anotherVariable = new someObjectType({
    property1:    "Some value for this named property",
    property2:    "This is the value for property 2"
});

... is generally better if there's more than 2/3 arguments, as it aids readability and makes it easier to avoid the optional argument problem (fn(null,null,null,123')).

Another consideration is performance. Passing arguments in the conventional way will be faster, but this speed gain only becomes significant in very performance-sensitive situations.

Can I use that second way to instantiate a variable of an object type that has been defined using the "classical"way of defining the object type with that implicit constructor?

Not easily. If you want to instantiate a constructor by using a hash instead of just passing arguments, and you don't have control over the source, then you could "wrap" it:

var _constructor = SomeConstructorFunction;

SomeConstructorFunction = function(hash) {
    return new _constructor(hash.property1, hash.property2);
};

I wouldn't really recommend messing with third-party APIs just for the sake of style though.

If I want to create an array of these objects, are there any other considerations?

How big is the array? What's the array for exactly? Performance might be worth considering...

like image 74
James Avatar answered Sep 24 '22 04:09

James