Why jslint prefers {} .constructor (obj) over Object (obj)

Both will detect objects, not primitives.

This seems to be a purely syntactical difference.

// jslint prefers {}.constructor(obj) over Object(obj) // called isObject by underscore // will test only for objects that have writable keys // for example string literals will not be detected // but arrays will var isWritable = function (obj) { return {}.constructor(obj) === obj; }; 
+8
javascript jslint
source share
2 answers

I'm not quite sure how I should not. If you look at performance, then this is the opposite of what you should do.

In accordance with this JSPerf test is available, which compares the creation speed using new Object() , Object.create().new() and Object.prototype.constructor() (which is the same as Object.constructor() ); Object.constructor() is the slowest of all .

The Google V8 engine runs faster with new Object() because it optimizes the call very much, so I really don't care about that.

JSPerf test results:

Operation per second comparison of Object creation functionsThe graph results of the speed of the different Object creation functions in multiple browsers

+4
source share

This is a false positive. Use Object , it is more concise, clearer and faster.

It is understood that “ JSLint does not expect to see a new Object . ” - you should instead use an object literal . However, the same warning is erroneously generated even if Object is called as a function . It may be the discovery of Object() creations, but there is nothing wrong with calling Object with an argument.

+1
source share

All Articles