admin管理员组文章数量:1244432
I have spent 10 or 20 minutes here and there probably a dozen times in the past year and never found a bulletproof answer to this question.
How do you check if a JavaScript object is an instance of Object
, not a subclass?
One use case for this is to check if arguments[0]
is an "options" hash vs. a "model" (MVC), both of which extend the native Object
, but which should be treated differently.
I have tried these:
// some helper to get constructor name
function klassName(fn) {
if (fn.__name__) {
return fn.__name__;
}
if (fn.name) {
return fn.name;
}
return fn.toString().match(/\W*function\s+([\w\$]+)\(/));
};
var Model = function() {};
m = new Model;
o = {};
Object(o) === o; // true
Object(m) === m; // true, thought it might be false
klassName(o.constructor); // Object
klassName(m.constructor); // Model
That klassName(m.constructor)
doesn't work in some cases (can't remember exactly, but maybe a regex.constructor, something like that). Maybe it does, don't know for sure.
Is there a bulletproof way to tell if something is an {}
object?
I have spent 10 or 20 minutes here and there probably a dozen times in the past year and never found a bulletproof answer to this question.
How do you check if a JavaScript object is an instance of Object
, not a subclass?
One use case for this is to check if arguments[0]
is an "options" hash vs. a "model" (MVC), both of which extend the native Object
, but which should be treated differently.
I have tried these:
// some helper to get constructor name
function klassName(fn) {
if (fn.__name__) {
return fn.__name__;
}
if (fn.name) {
return fn.name;
}
return fn.toString().match(/\W*function\s+([\w\$]+)\(/));
};
var Model = function() {};
m = new Model;
o = {};
Object(o) === o; // true
Object(m) === m; // true, thought it might be false
klassName(o.constructor); // Object
klassName(m.constructor); // Model
That klassName(m.constructor)
doesn't work in some cases (can't remember exactly, but maybe a regex.constructor, something like that). Maybe it does, don't know for sure.
Is there a bulletproof way to tell if something is an {}
object?
-
1
In JavaScript nothing is bulletproof. Not even the
Bulletproof
function itself. :) – Kendall Frey Commented Apr 25, 2012 at 23:27 - 1 BTW - JavaScript does not have a subclass per-se. Only constructor functions and prototypes. – gnarf Commented Apr 25, 2012 at 23:58
3 Answers
Reset to default 12Might something as simple as
function isObj( test ) {
return test.constructor === Object;
}
Be what you are looking for?
Test in jsFiddle
You mean this? http://jsfiddle/elclanrs/ukEEw/
var o = {};
var re = /\d/;
var f = function(){};
var d = new Date();
var isObj = function(e){
return e.toString() === '[object Object]';
};
console.log( isObj(o) ); // True
console.log( isObj(re) ); // False
console.log( isObj(f) ); // False
console.log( isObj(d) ); // False
I would just change one thing
var isObj = function(e){
if(!e) return false; // change this line
return e.toString() === '[object Object]';
};
本文标签: How do you check if a JavaScript object is directly aninstancenot a subclassStack Overflow
版权声明:本文标题:How do you check if a JavaScript object is directly an `{}` instance, not a subclass? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740148418a2232209.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论