2011-09-19 4 views

Répondre

7

En utilisant typeof opérateur, vous pouvez déterminer les éléments suivants:

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

Modifié: Comme il a été suggéré dans un commentaire, vous pouvez également vérifier La valeur f est nulle, car typeof null renvoie l'objet.

+4

croyiez ou non, '' typeof null' est 'object''. Crockford a une journée sur le terrain avec ce ... –

1

Effectuez les opérations suivantes

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

Vous pouvez utiliser typeof:

typeof 5 == "number"; 
typeof 1.5 == "number"; 
typeof true == "boolean"; 
typeof "word" == "string"; 
typeof {} == "object"; 

En gros:

if(obj == null) { 
    //null or undefined 
} 
else if(typeof obj == "object") { 
    //It's "complex" 
} 
else { 
    //Primitive or "simple" 
} 

Note: null retournera "object", donc vous devez vérifier pour elle.

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

Dans votre cas:

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

Comme ce que je comprends de vous la question - vous avez besoin de savoir si l'objet a des propriétés/méthodes.

1

Le problème est que plus que {} retourne un type de 'objet'

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' 

Mais, en utilisant toString vous pouvez vérifier plus:

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

Donc, la meilleure façon de chèque est:

var test; 

test = {}; 
typeof test == 'object' && toString.call(test) == '[object Object]'; // true 

test = []; 
typeof test == 'object' && toString.call(test) == '[object Object]'; // false 

// et cetera 

espoir qui aide

0

Vous pouvez tout simplement faire une fonction simple qui renvoie true pour les types simples:

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

Non que cela retourne vrai pour NaN car il est considéré comme un numéro, et faux pour « undefined » - mais vous pouvez facilement modifier cette en fonction de votre cas particulier.

Exécutez l'extrait ci-dessous pour le voir en action

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

Questions connexes