/* 方法一:函數法 */ 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