JavaScript:獲取數據の類型

/* 方法一:函數法 */
function type(o){return Object.prototype.toString.call(o).slice(8, -1);}
/* 方法二:prototype法 */
Object.prototype.__defineGetter__('type', function() {return Object.prototype.toString.call(this).slice(8, -1);});

prototype法使用說明:

舉例:

var a="s",b=3,c={},d=[],e=new Set();

console.log (a.type);  //輸出String

console.log (b.type);  //輸出Number

console.log (c.type);  //輸出Object

console.log (d.type);  //輸出Array

console.log (e.type);  //輸出Set
posted @ 2019-03-08 11:04  漫冬飄雪  阅读(82)  评论(0编辑  收藏  举报