function isObject(o) {
return null != o &&
typeof o === 'object' &&
Object.prototype.toString.call(o) === '[object Object]';
}
function isDerivedObject(o) {
return !isObject(o) &&
null != o &&
(typeof o === 'object' || typeof o === 'function') &&
/^\[object /.test(Object.prototype.toString.call(o));
}
// Loose equality operator (==) is intentionally used to check
// for undefined too
// Also note that, even null is an object, within isDerivedObject
// function we skip that and always return false for null
In Javascript, null
, Object
, Array
, Date
and function
s are all objects. Although, null
is bit contrived. So, it's better to check for the null
first, to detect it's not null.
Checking for typeof o === 'object'
guarantees that o
is an object. Without this check, Object.prototype.toString
would be meaningless, since it would return object for everthing, even for undefined
and null
! For example: toString(undefined)
returns [object Undefined]
!
After typeof o === 'object'
check, toString.call(o) is a great method to check whether o
is an object, a derived object like Array
, Date
or a function
.
In isDerivedObject
function, it checks for the o
is a function. Because, function also an object, that's why it's there. If it didn't do that, function will return as false. Example: isDerivedObject(function() {})
would return false
, however now it returns true
.
One can always change the definition of what is an object. So, one can change these functions accordingly.
function isObject(o) {_x000D_
return null != o && _x000D_
typeof o === 'object' && _x000D_
Object.prototype.toString.call(o) === '[object Object]';_x000D_
}_x000D_
_x000D_
function isDerivedObject(o) {_x000D_
return !isObject(o) && _x000D_
null != o && _x000D_
(typeof o === 'object' || typeof o === 'function') &&_x000D_
/^\[object /.test(Object.prototype.toString.call(o));_x000D_
}_x000D_
_x000D_
// TESTS_x000D_
_x000D_
// is null an object?_x000D_
_x000D_
console.log(_x000D_
'is null an object?', isObject(null)_x000D_
);_x000D_
_x000D_
console.log(_x000D_
'is null a derived object?', isDerivedObject(null)_x000D_
);_x000D_
_x000D_
// is 1234 an object?_x000D_
_x000D_
console.log(_x000D_
'is 1234 an object?', isObject(1234)_x000D_
);_x000D_
_x000D_
console.log(_x000D_
'is 1234 a derived object?', isDerivedObject(1234)_x000D_
);_x000D_
_x000D_
// is new Number(1234) an object?_x000D_
_x000D_
console.log(_x000D_
'is new Number(1234) an object?', isObject(new Number(1234))_x000D_
);_x000D_
_x000D_
console.log(_x000D_
'is new Number(1234) a derived object?', isDerivedObject(1234)_x000D_
);_x000D_
_x000D_
// is function object an object?_x000D_
_x000D_
console.log(_x000D_
'is (new (function (){})) an object?', _x000D_
isObject((new (function (){})))_x000D_
);_x000D_
_x000D_
console.log(_x000D_
'is (new (function (){})) a derived object?', _x000D_
isObject((new (function (){})))_x000D_
);_x000D_
_x000D_
// is {} an object?_x000D_
_x000D_
console.log(_x000D_
'is {} an object?', isObject({})_x000D_
);_x000D_
_x000D_
console.log(_x000D_
'is {} a derived object?', isDerivedObject({})_x000D_
);_x000D_
_x000D_
// is Array an object?_x000D_
_x000D_
console.log(_x000D_
'is Array an object?',_x000D_
isObject([])_x000D_
)_x000D_
_x000D_
console.log(_x000D_
'is Array a derived object?',_x000D_
isDerivedObject([])_x000D_
)_x000D_
_x000D_
// is Date an object?_x000D_
_x000D_
console.log(_x000D_
'is Date an object?', isObject(new Date())_x000D_
);_x000D_
_x000D_
console.log(_x000D_
'is Date a derived object?', isDerivedObject(new Date())_x000D_
);_x000D_
_x000D_
// is function an object?_x000D_
_x000D_
console.log(_x000D_
'is function an object?', isObject(function(){})_x000D_
);_x000D_
_x000D_
console.log(_x000D_
'is function a derived object?', isDerivedObject(function(){})_x000D_
);
_x000D_