基本上我需要分清以下两点:
var simple = 5 // or "word", or 56.78, or any other "simple" object
var complex = {propname: "propvalue", "otherprop": "othervalue"}
答案 0 :(得分:8)
使用typeof
运算符可以确定以下内容:
"number" Operand is a number
"string" Operand is a string
"boolean" Operand is a Boolean
"object" Operand is an object
"undefined" Operand is not defined.
<强>编辑:强>
正如在评论中建议的那样,您可能还想检查值是否为空,因为typeof null
将返回对象。
答案 1 :(得分:3)
您可以使用typeof
:
typeof 5 == "number";
typeof 1.5 == "number";
typeof true == "boolean";
typeof "word" == "string";
typeof {} == "object";
基本上:
if(obj == null) {
//null or undefined
}
else if(typeof obj == "object") {
//It's "complex"
}
else {
//Primitive or "simple"
}
注意:null
将返回"object"
,因此您需要检查它。
答案 2 :(得分:1)
尝试以下
if (typeof obj === 'object') {
// It's complex
} else {
// It's not
}
答案 3 :(得分:1)
Object.prototype.getName = function() {
var funcNameRegex = /function (.{1,})\(/;
var results = (funcNameRegex).exec((this).constructor.toString());
return (results && results.length > 1) ? results[1] : "";
};
var simple = 5; // or "word", or 56.78, or any other "simple" object
var complex = { propname : "propvalue"
, "otherprop" : "othervalue"
};
simple.getName(); // returns: "Number"
complex.getName(); // returns: "Object"
答案 4 :(得分:1)
问题是,不只是{}返回一种'对象'
typeof 5 == 'number'
typeof NaN == 'number'
typeof 'test' == 'string'
typeof true == 'boolean'
typeof undefined == 'undefined'
typeof null == 'object'
typeof /asdf/ == 'object' // this is true in some engines, like Firefox's. Not in V8 (in which it is 'function')
typeof [] == 'object'
typeof {} == 'object'
但是,通过使用toString,您可以进一步检查:
toString.call(null) == '[object Window]' // or '[object global]' or '[object Null]' - depends on engine
toString.call(/asdf/) == '[object RegExp]'
toString.call([]) == '[object Array]'
toString.call({}) == '[object Object]'
因此,最好的检查方法是:
var test;
test = {};
typeof test == 'object' && toString.call(test) == '[object Object]'; // true
test = [];
typeof test == 'object' && toString.call(test) == '[object Object]'; // false
// et cetera
希望有所帮助
答案 5 :(得分:0)
在你的情况下:
var simple = 5; // number, not an object
var simple = new Number(5); // now it is an object, but still the value is 5
var complex = {propname: "propvalue", "otherprop": "othervalue"};
for ( property in complex ) {
if ( complex.hasOwnProperty( property ) )
{
alert ( 'composite object' );
return;
} else {
alert ( 'simple object' );
return;
}
}
根据我对你的理解 - 你需要告诉对象是否有属性/方法。
答案 6 :(得分:0)
你可以创建一个简单函数,对简单类型返回true:
function isSimple( a ) {
return (typeof a).match(/(number)|(boolean)|(string)/)
}
对于NaN
而言,这不会返回true,因为它被认为是一个数字,而对于'undefined'则为false - 但您可以轻松修改它以适合您的具体情况。
运行下面的代码段以查看其实际效果
<script>
// return true/false if typeof matches simple regex pattern
function isSimple( a ) {
return (typeof a).match(/(number)|(boolean)|(string)/);
}
// setup some tests cases
var tests = [
[1,2,3],
'hello',
7,
{ foo: 'bar' },
NaN
]
// log output of isSimple function against each test case
for( var i in tests ) {
var value = tests[ i ];
if( isSimple( value ) ) {
console.log( 'simple value', value );
} else {
console.log( 'not simple', value );
}
}
</script>