I'm curious, becouse i thought that
Object.prototype.toString.call(null)
return [object Object]
, but now i was checking that in Chrome and FF and both returned [object Null]
. Now the question is, wether i can assume that Object.prototype.toString will always tell me the good type?
Until now, i was checking every type with this method, but not for null values, i was checking null values by
obj === null;
Thanks!
Note for clarification: this 'problem' is not a serious case, since atm i'm using
function isNull(obj) {
return obj === null;
}
function isUndefined(obj) {
return typeof obj === 'undefined';
}
which works just fine, but if Object.prototype.toString.call()
would work sufficient in older browsers, i could drop these two functions and extend my solution with null
and undefined
like:
var types = ['Object', 'Number', 'String', 'Array', 'Function',
'Date', 'RegExp', 'Boolean', 'Error', 'Null', 'Undefined'].reduce(function(prev, type) {
prev['[object ' + type + ']'] = type.toLowerCase();
return prev;
}, {});
function isType(type) {
return function(obj) {
return getType(obj) === type;
};
}
function getType(obj) {
return types[Object.prototype.toString.call(obj)];
}
var isNull = isType('null');
document.querySelector('#console').textContent = isNull(null);
<div id='console'></div>