2011-09-19 89 views

回答

7

使用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. 

編輯: 正如評論中所建議的,你可能也想檢查我f值爲空,因爲typeof null將返回對象。

+4

不管你信不信,'的typeof null'是''object''。克羅克福德有一個與這個領域的一天... –

1

請嘗試以下

if (typeof obj === 'object') { 
    // It's complex 
} else { 
    // It's not 
} 
2

你可以使用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",所以你需要檢查它。

1

Credit here

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" 
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; 
    } 
} 

由於我的理解,從你的問題 - 你需要告訴如果對象的屬性/方法。

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 

希望幫助

0

你可能只是做一個簡單的功能,對於簡單類型返回true:

function isSimple(a) { 
    return (typeof a).match(/(number)|(boolean)|(string)/) 
} 

不,因爲它被認爲是一個數字,而假的「未定義」,這將返回true爲NaN - 但你可以很容易地修改此以適應您的具體情況。

運行下面的代碼片段,看看它在行動

<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>

相關問題