JavaScript入門データ型の判定方法まとめ

1.typeof
alert(typeof a) ————> string
alert(typeof b) ————> number
alert(typeof c) ————> object
alert(typeof d) ————> object
alert(typeof e) ————> function
alert(typeof f) ————> function

2. constructor
function A(){};
function B(){};
A.prototype = new B();
var ctnOb = new A();
alert(aobj.constructor === B) ———–> true;
alert(aobj.constructor === A) ———–> false;

3. prototype
alert(Object.prototype.toString.call(a) === ‘[object String]’) ——-> true;
alert(Object.prototype.toString.call(b) === ‘[object Number]’) ——-> true;
alert(Object.prototype.toString.call(c) === ‘[object Array]’) ——-> true;
alert(Object.prototype.toString.call(d) === ‘[object Date]’) ——-> true;
alert(Object.prototype.toString.call(e) === ‘[object Function]’) ——-> true;
alert(Object.prototype.toString.call(f) === ‘[object Function]’) ——-> true;
4.jquery.type()
jQuery.type( undefined ) === “undefined"
jQuery.type() === “undefined"
jQuery.type( window.notDefined ) === “undefined"
jQuery.type( null ) === “null"

jQuery.type( true ) === “boolean"
jQuery.type( 6 ) === “number"
jQuery.type( “test" ) === “string"
jQuery.type( function(){} ) === “function"
jQuery.type( [] ) === “array"
jQuery.type( new Date() ) === “date"
jQuery.type( new Error() ) === “error" // as of jQuery 1.9
jQuery.type( /test/ ) === “regexp"

5. instanceof
alert(c instanceof Array) —————> true
alert(d instanceof Date)
alert(f instanceof Function) ————> true
alert(f instanceof function) ————> false

JavaScript

Posted by arkgame