Type | Result |
---|---|
Undefined | "undefined" |
Null | "object" |
Boolean | "boolean" |
Number | "number" |
String | "string" |
Host object (provided by the JS environment) | Implementation-dependent |
Function object (implements [[Call]] in ECMA-262 terms) | "function" |
Any other object | "object" |
typeof "abc" // "string"
typeof {} // "object"
typeof true // "boolean"
typeof undefined // "undefined"
typeof function(){} // "function"
typeof [] // "object"
typeof null // "object"
=================================================
* instanceof operator
function Animal(){}
var a = new Animal()
a instanceof Animal // true
a.constructor === Animal // true
function Cat(){}
Cat.prototype = new Animal
Cat.prototype.constructor = Cat
var felix = new Cat
felix instanceof Cat // true
felix instanceof Animal // true
felix.constructor === Cat // true
felix.constructor === Animal // false
felix = null
felix instanceof Animal // true
felix.constructor === Animal // throws TypeError
-------------------------------------------------
[1, 2, 3] instanceof Array // true
/abc/ instanceof RegExp // true
({}) instanceof Object // true
(function(){}) instanceof Function // true
null instanceof Boolean // false
undefined instanceof Array // false
3 instanceof Number // false
true instanceof Boolean // false
'abc' instanceof String // false
(3).constructor === Number // true
true.constructor === Boolean // true
'abc'.constructor === String // true
출처 :
http://tobyho.com/2011/01/28/checking-types-in-javascript/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof
댓글 없음:
댓글 쓰기