javascript - What does Object(obj) === obj do? -


different obj != null;

i know obj != null detect allowed have properties on null , undefined 2 values can not have properties.

how differ from

object(obj) === obj;

object(obj) === obj tests whether obj object or primitive, failing strings, etc.

console.log(object('foo') === 'foo'); // false console.log(object(true) === true);   // false console.log(object(null) === null);   // false  var obj = {}; console.log(object(obj) === obj);     // true 

it's useful determining if value can given and remember assigned property.

while null , undefined outright error when attempting use properties, why obj != null still useful, no primitive values able hold onto properties.

var pri = 'foo'; pri.foo = 'bar';      // no error, still... console.log(pri.foo); // undefined  var box = object(pri); box.foo = 'bar'; console.log(box.foo); // 'bar' 

reference:

when obj null or undefined, object(obj) returns new object():

1) if value null, undefined or not supplied, create , return new object object if standard built-in object constructor had been called same arguments (15.2.2.1).

and, primitive booleans, strings, , numbers boxed object types via toobject(), not equal primitive equivalents:

2) return toobject(value).

console.log(typeof 'foo' === 'string');         // true console.log(typeof object('foo') === 'object'); // true  console.log('foo' instanceof string);           // false console.log(object('foo') instanceof string);   // true 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -