4

I've been talking to some professionals who work with JavaScript in a company, and I've been told that it isn't a good practice to create an object instance using either new or the global object {}, even if I want an empty object, like this:

var wrong1 = new Object();
var wrong2 = {};

The correct way, according to them and the company's standards, is to create it like this:

var correct = Object.create({});

Passing an empty object as the prototype of an empty object seems rather over-engineered, and maybe even purposeless.

Can someone provide me an answer as to why this is recommended, or if it isn't, why not? (possible pros. and cons.)

pseudosudo
  • 23
  • 5
Bruno Finger
  • 2,105
  • 3
  • 27
  • 47
  • 5
    But isn't passing empty object literal `{}` wrong according to those guidelines? Wouldn't `Object.create(Object.create({}))` be better? ;) – el.pescado - нет войне May 31 '16 at 11:52
  • Possible duplicate of [What is the difference between \`new Object()\` and object literal notation?](http://stackoverflow.com/questions/4597926/what-is-the-difference-between-new-object-and-object-literal-notation) – Oleg May 31 '16 at 11:56
  • 3
    @el.pescado - that's good, but this is bullet proof: `function create(){return Object.create(create())}` – Lee Gunn May 31 '16 at 11:57
  • There's no such thing as "global object {}". – Oleg May 31 '16 at 11:58
  • what is an empty object? one without any prototypes? – Nina Scholz May 31 '16 at 12:00
  • @Oleg don't see how the duplicate link you provided is a duplicate. Doesn't really discuss Object.create. Related yes...duplicate no – charlietfl May 31 '16 at 12:00
  • @charlietfl I've seen similar questions a few times already. People are wondering about the best way to create objects. Use `var a = {};`, it has been answered many times already. – Oleg May 31 '16 at 12:04
  • 1
    @Oleg but that's not what is being asked here – charlietfl May 31 '16 at 12:05
  • @Oleg what is being asked here is *why* would this be a standard practice within a software company, I mean, the real advantages and the disadvantages if any. I believe it is an important concept. – Bruno Finger May 31 '16 at 12:09
  • @Oleg Furthermore, the question you linked barely references `Object.create` while here I am explicitly asking about it. – Bruno Finger May 31 '16 at 12:13
  • 2
    There might be some cases when you want your object to have no prototype using `Object.create(null)`. For example if you need some sort of caching and you want to avoid name collisions and don't want to use `hasOwnProperty`. But `Object.create({})` looks like a complete overhead. – Yury Tarabanko May 31 '16 at 12:13

1 Answers1

6

The object literal will create an object with the following prototype chain.

Object.prototype -> {}

The literal syntax is heavily optimized by most engines because it's such a common pattern.

To compare, Object.create({}) creates an object with a longer prototype chain.

Object.prototype -> {} -> {}

The instance (on the far right) has a prototype of {}, which as seen before already has a prototype of Object.prototype.

Object.create is a very useful function for creating objects with custom prototypes and it doesn't suffer from the same problems as new. However, we don't need it here because {} already creates an object with a shorter prototype chain.

I don't know why anyone would think that adding this extra layer of indirection (and performance loss) was a "best practice".

If anything, I'd say that the literal syntax was safer—as there's no way for {} to be accidentally/maliciously redefined, unlike Object which can be shadowed.

var Object = 3;
// ...
Object.create({}) // TypeError: Object.create is not a function(...)

Of course, this doesn't mean you should never use Object, but in this case I think that {} is the simplest tool for the job and as a result, it has the best performance too.

Dan Prince
  • 29,491
  • 13
  • 89
  • 120
  • You have a really good point. `Object` can be shadowed and so can the `.create` function. – Bruno Finger May 31 '16 at 12:07
  • 1
    My guess is someone was being really anal and decided that if `Object.create()` exists...then it simply has to be better – charlietfl May 31 '16 at 12:08
  • So by this comment, I can assume that `var a = {}` is even safer than `var b = new Object()`? – Bruno Finger May 31 '16 at 12:10
  • @BrunoFinger Yep. Prefer literal syntax when possible, it's the easiest to read (for other programmers) and optimise (for the compiler). – Dan Prince May 31 '16 at 12:13
  • 1
    Moral of the story: don't let people tell you what a best practise is without a solid justification as to why it is one. – Jonathan May 31 '16 at 12:19