8

Both will detect objects and not primitives.

It seems like 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;
};
Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237
  • I think it may have something to do with how fast the javascript runs. `{}.constructor(obj)` probably has some sort of optimization in most javascript engines that `Object(obj)` doesn't have. – RevanProdigalKnight Jul 01 '14 at 13:18
  • @user, do you mean `({}).constructor(obj)`? Plain `{}.constructor(obj)` results in a syntax error on my browser. – Frédéric Hamidi Jul 01 '14 at 13:22
  • The code I posted above works fine. But when I break it up and put it in the console I get the same error you did. Weird. –  Jul 01 '14 at 13:32
  • 1
    @user, never mind, that's the `return` keyword. It prevents the parser from interpreting `{}` as an empty statement block. That code indeed runs fine. – Frédéric Hamidi Jul 01 '14 at 13:38
  • The only thing I could possibly think of is that `Object` is not a reserved word, so a jerk could say: `Object = function(x) { /* be a jerk */ }` where as `{}.constructor` will always get you the object constructor. – J. Holmes Jul 01 '14 at 14:35

2 Answers2

4

I'm not quite sure, as it shouldn't. If you're looking at performance, then that's the opposite of what you should be doing.

According to this JSPerf test available which compares the speed of creating via new Object(), Object.create().new(), and Object.prototype.constructor() (which is the same as Object.constructor()); Object.constructor() is the slowest one of them all by far.

Google's V8 engine is magnitudes faster using the new Object() because it optimizes the call so heavily, so I really wouldn't worry about it.

Results from the JSPerf test:

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

Evan Darwin
  • 850
  • 1
  • 8
  • 29
1

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

It is understandable that "JSLint does not expect to see new Object." - you should use an object literal instead. However, the same warning is mistakenly issued even if Object is called as a function. This might be to detect Object() creations, but there is nothing wrong with calling Object with an argument.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • You should file a bug to jslint, this looks like one. – Benjamin Gruenbaum Jul 01 '14 at 15:58
  • 1
    @BenjaminGruenbaum: I've sent an email, looks like there is no bug tracker. – Bergi Jul 01 '14 at 16:19
  • …but all I got back was the comment "*No. Don't do that.*". So to make JSLint happy, we seem to should a) alias `Object` with some other name (`var o = Object; return o(obj) === obj;`), or b) use the only little longer `(obj !== null && typeof obj === "object") || typeof obj === "function";` – Bergi Jul 01 '14 at 22:37