Creating new objects in JavaScript

I'm relatively new to object-oriented programming in JavaScript, and I'm not sure about the โ€œbestโ€ way to define and use objects in JavaScript. I saw a โ€œcanonicalโ€ way of defining objects and instantiating 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 saw other ways to create new instances of objects this way:

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

I like how the second method appears - the code itself is documented. But my questions are:

  • Which way is "better"?

  • Can I use this second way to instantiate an object variable of a type that was defined using the "classic" way of determining the type of an object with this implicit constructor?

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

Thanks in advance.

+7
javascript javascript-objects
source share
4 answers

It really tastes good. Thus:

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

... it is usually better if there are more than 2/3 of the arguments, as it helps readability and simplifies the elimination of an optional problem with the argument ( fn(null,null,null,123') ).

Another consideration is performance. Passing arguments in the usual way will be faster, but this increase in speed becomes significant only in situations with high sensitivity.

Can I use this second way of instantiating an object type variable that was defined using the "classic" way of defining an object type using an implicit constructor?

Not easy. If you want to instantiate a constructor using a hash instead of simply passing arguments, and you have no control over the source, you can wrap it:

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

I really would not recommend messing with third-party APIs just for the sake of style.

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

How big is the array? Which array exactly? Performance may be worth attention ...

+7
source share

The best way to create javascript objects is to refuse to use new ones (at least if you are subscribed to crockford camp)

 myObjectType = function(props) { // anything defined on props will be private due to the closure props.privateVar = "private"; // anything defined on obj will be public obj = {}; obj.testing = new function() { alert(props.privateVar); }; return obj; }; instance = myObjectType({prop1: "prop"}); // if we want inheritance, it just means starting with the base type instead of // a new object subType = function(props) { obj = myObjectType(props); obj.subTypeProperty = "hello."; return obj; }; 

Page 52 of Javascript: Good Parts , I Highly Recommend :-)

+3
source share

Well, the second way looks beautiful and useful in the case of a โ€œclassโ€ that has many settings. However, keep in mind that you are actually creating another object in the process.

I would advise you to read the code from one or another Javascript structure and find a style that you like.

+1
source share

1) I would say that method No. 2 is very preferable for me. An example with two properties is not that different, but what would you like to do:

 var anotherVariable = new someObjectType({ property1: "Some value for this named property", property2: "This is the value for property 2" //Leaving several properties out, don't want them populated property8: "This is the value for property 8" property9: "This is the value for property 9" }); 

Consider how many combinations of overloads (or null s tracking) you have to handle properties that you may or may not want to provide to the object with the first method. This is a much more extensible and flexible approach.

2) Just enable it with an empty constructor, it will be much cleaner to instantiate.

3) Length / readability, especially with multiple objects. Look at JSON, it's pretty clean / readable, then again, at least for me, if you like this style, creating arrays of your objects is very similar to method # 2.

+1
source share

All Articles