Created
March 9, 2011 12:04
-
-
Save think49/862085 to your computer and use it in GitHub Desktop.
get-type.js : ECMAScript5 準拠のデータ型を返す
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* get-type.js | |
* | |
* @version 1.0.2 | |
* @author think49 | |
* @url https://gist.github.com/862085 | |
* @license http://www.opensource.org/licenses/mit-license.php (The MIT License) | |
*/ | |
function getType (value) { | |
var type, typeofValue; | |
if (value === null) { // Null Type | |
return 'Null'; | |
} | |
typeofValue = typeof value; | |
switch (typeofValue) { | |
case 'undefined': // Undefined Type | |
case 'boolean': // Boolean Type | |
case 'number': // Number Type | |
case 'string': // String Type | |
type = typeofValue.charAt(0).toUpperCase() + typeofValue.slice(1); | |
break; | |
case 'object': // Object Type (native and does not implement [[Call]]) | |
case 'function': // Object Type (native or host and does implement [[Call]]) | |
default: // Object Type (host and does not implement [[Call]]) | |
type = 'Object'; | |
break; | |
} | |
return type; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
get-type.js
使い方
第一引数で渡した値の 型 を返します。
Object 型だけを判定したいなら
typeof 演算子で判定が面倒なのは Object 型だけなので、Object 型以外の判定は独自の関数を使う必要がないかもしれません。
is-object.js を使えば、対象が Object 型かどうか判定できます。
参考リンク
ECMAScript 3
ECMAScript 5.1
その他