I believe that both of the objects end up being created in a similar manner, and the difference in performance (if any) will be minute to the point where you shouldn't notice a difference.
This is not universal, though. When creating arrays, it is more efficient to use a similar approach to B. This is because (at least with V8) allocating an array with a set size is more efficient because it knows the size of the array.
// Here V8 can see that you want a 4-element array containing numbers:
var a = [1, 2, 3, 4];
// Don't do this:
a = []; // Here V8 knows nothing about the array
for(var i = 1; i <= 4; i++) {
a.push(i);
}
Source: Smashing Magazine
I would suggest trying out the Chrome Profiling Tools to see if you can notice a difference between the efficiency of both approaches, although I think if there is a difference it will be too small to make a difference.
Further more, if you are new to JavaScript, I would suggest checking out the awesome JavaScript style guide created by AirBnB. There are a ton of great tips for writing efficient and clean JavaScript.
field2based onfield1; you can't exactly do that in the object literal initialization.newoperator on a constructor function:var obj = new (function Object() {this.field1 = /* some expression */; this.field2 = /* some expression */; var privateProperty = "somevalue"; this.field3 = this.field1+privateProperty; })();This allows you hide some properties if that's required.Object.createdeveloper.mozilla.org/en-US/docs/Web/JavaScript/Reference/…