- 追加された行はこの色です。
- 削除された行はこの色です。
* 型を調べる [#k7068d01]
** typeof演算子[#z50b9a8e]
プリミティブ型の型名を取得する。
if (typeof(foo) == "boolean" ) {
...
}
- string
- number
- boolean
- function
- object
- undefined
typeof [1, 2, 3] // => "object"
typeof parseInt("a") // => "number"
** instanceof演算子[#b4791a98]
var obj = {};
var arr = [];
obj instanceof Object // => true
obj instanceof Array // => false
arr instanceof Object // => true
arr instanceof Array // => true
** Objecet型の詳細 [#m3d2734b]
** constructor.toString() [#e43d2032]
以下のようなオブジェクトがあるとして、
function Foo(v){ this.v = v; };
foo = new Foo();
bar = { BAR: 1 };
以下のようなFoo/Bar型のオブジェクトがあるとして、
function Foo(){ 1; }
foo = new Foo;
bar = { 2; };
typeof演算子では結果は
typeof foo // => "function"
typeof演算子の結果はobjectしか分からない。
typeof foo // => "object"
typeof bar // => "object"
obj.constructor.toString();
その詳細を知るには以下のようにconstructor.toString()を使う。
foo.constructor.toString(); // => "function Foo(v){ this.v = v; }"
bar.constructor.toString(); // => "function Object() { [native code] }
"function Foo(){ console.log(1); }"