This is used to determine if something is a primitive or not.

`typeof null` returns 'object' even though it is not an object and `typeof 
function() {}` returns 'function' even though it is an object. Basically 
'typeof' alone is not enough to determine if something is primitive or 
object. `instanceof Object` has some edge cases for instance if an object 
is created with `Object.create(null)` or is from another global context.

On Friday, September 19, 2014 1:00:19 AM UTC-7, enormouspenguin wrote:
>
> if (obj === Object(obj)) return 'object';
>
> That is cut from chaijs/type-detect/blob/master/lib/type.js#L42 
> <https://github.com/chaijs/type-detect/blob/master/lib/type.js#L42>.
>
> My first guess (probably a joke):
>
> obj === Object(obj)
>
> is somewhat similar to
>
> obj instanceof Object
>
> My second guess is that it's used to distinguish between primitive value 
> (created using literal) and primitive object (created using instantiation) 
> such as 1 and (new Number(1)).
>
> But typeof operator could correctly distinguish by itself:
>
> typeof 1 === 'number'
> typeof new Number(1) === 'object'
>
> Also I feel line 41 
> <https://github.com/chaijs/type-detect/blob/master/lib/type.js#L41> is 
> unnecessary too:
>
> if (obj === undefined) return 'undefined';
>
> I don't know why would those 2 lines of code be there while
>
> return typeof obj;
>
> in line 43 
> <https://github.com/chaijs/type-detect/blob/master/lib/type.js#L43> could 
> as well be enough to substitute both.
>
> Is there any edge case here?
>
> May be I am missing something internal here?
>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/02aa5b22-b196-44ee-abb4-3042fbbe9456%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to