1

How to avoid jslint to shout at me with the warning

'Expected 'Object.create(null)' and instead saw 'new Object'

when using this very simple line of code:

var myDummyObject = new Object();

?

Natalie Perret
  • 8,013
  • 12
  • 66
  • 129
  • Not sure why jslint doesn't like that, but you can do `var myDummyObject = {};` It may, in general not like the `new Object()` and `new Array()` forms and prefer the static declarations such as `{}` and `[]`; `Object.create(null)` creates a special object with an empty prototype so it won't have methods such as `.hasOwnProperty()` which is sometimes useful, but usually not required. – jfriend00 Jun 24 '17 at 23:59
  • @jfriend00 yup I know jslint is fine with other forms, was just curious about why it complains that one in particular. – Natalie Perret Jun 25 '17 at 00:03
  • 3
    Well, jslint is opinionated and you use it because you want to know its opinion about things. Apparently, it has an opinion that `new Object()` should not be used. The issue you see is documented here: http://www.jslint.com/help.html. It does not say why. Lots of discussion about it here: [What is the difference between `new Object()` and object literal notation?](https://stackoverflow.com/questions/4597926/what-is-the-difference-between-new-object-and-object-literal-notation). – jfriend00 Jun 25 '17 at 00:10

1 Answers1

3

Well, jslint is opinionated and you use it because you want to know its opinion about things. Apparently, it has an opinion that new Object() should not be used. The issue you see is documented here: http://www.jslint.com/help.html. It does not say why.

Lots of discussion about it here: What is the difference between new Object() and object literal notation?. The {} syntax is both faster and shorter than new Object() which sounds like enough of a reason to prefer it.

As you apparently know, you can use either of these:

var myDummyObject = {};
var myDummyObject = Object.create(null);

The latter will create an object with an empty prototype so it won't have methods on it like .hasOwnProperty() which is occasionally useful though usually not needed.

jfriend00
  • 683,504
  • 96
  • 985
  • 979