Ail*_*lyn 246 javascript types typeof
有没有更好的方法来获取JS中的变量类型typeof?当你这样做时它工作正常:
> typeof 1
"number"
> typeof "hello"
"string"
Run Code Online (Sandbox Code Playgroud)
但是当你尝试时它没用了:
> typeof [1,2]
"object"
>r = new RegExp(/./)
/./
> typeof r
"function"
Run Code Online (Sandbox Code Playgroud)
我知道instanceof,但这要求你事先知道这种类型.
> [1,2] instanceof Array
true
> r instanceof RegExp
true
Run Code Online (Sandbox Code Playgroud)
有没有更好的办法?
ipr*_*101 214
安格斯·克罗尔最近写了一篇有趣的博客文章 -
http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
他详细介绍了各种方法的优点和缺点,然后定义了一种新方法'toType' -
var toType = function(obj) {
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
}
Run Code Online (Sandbox Code Playgroud)
Ale*_*pin 48
你可以尝试使用constructor.name.
[].constructor.name
new RegExp().constructor.name
Run Code Online (Sandbox Code Playgroud)
就像所有的JavaScript一样,有人最终总会指出这是某种邪恶的,所以这里有一个链接到一个很好地涵盖这个问题的答案.
另一种方法是使用 Object.prototype.toString.call
Object.prototype.toString.call([])
Object.prototype.toString.call(/./)
Run Code Online (Sandbox Code Playgroud)
小智 38
一个相当不错的类型捕获函数是YUI3使用的函数:
var TYPES = {
'undefined' : 'undefined',
'number' : 'number',
'boolean' : 'boolean',
'string' : 'string',
'[object Function]': 'function',
'[object RegExp]' : 'regexp',
'[object Array]' : 'array',
'[object Date]' : 'date',
'[object Error]' : 'error'
},
TOSTRING = Object.prototype.toString;
function type(o) {
return TYPES[typeof o] || TYPES[TOSTRING.call(o)] || (o ? 'object' : 'null');
};
Run Code Online (Sandbox Code Playgroud)
这捕获了javascript提供的许多基元,但您总是可以通过修改TYPES对象来添加更多基元.请注意,typeof HTMLElementCollection在Safari中将报告function,但类型(HTMLElementCollection)将返回object
Vix*_*Vix 30
您可能会发现以下功能有用:
function typeOf(obj) {
return {}.toString.call(obj).split(' ')[1].slice(0, -1).toLowerCase();
}
Run Code Online (Sandbox Code Playgroud)
或者在ES7中(如果进一步改进则发表评论)
function typeOf(obj) {
const { toString } = Object.prototype;
const stringified = obj::toString();
const type = stringified.split(' ')[1].slice(0, -1);
return type.toLowerCase();
}
Run Code Online (Sandbox Code Playgroud)
结果:
typeOf(); //undefined
typeOf(null); //null
typeOf(NaN); //number
typeOf(5); //number
typeOf({}); //object
typeOf([]); //array
typeOf(''); //string
typeOf(function () {}); //function
typeOf(/a/) //regexp
typeOf(new Date()) //date
typeOf(new Error) //error
typeOf(Promise.resolve()) //promise
typeOf(function *() {}) //generatorfunction
typeOf(new WeakMap()) //weakmap
typeOf(new Map()) //map
Run Code Online (Sandbox Code Playgroud)
感谢@johnrees通知我:错误,承诺,生成器功能
小智 14
我们也可以从ipr101改变一个小例子
Object.prototype.toType = function() {
return ({}).toString.call(this).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
}
Run Code Online (Sandbox Code Playgroud)
并打电话给
"aaa".toType(); // 'string'
Run Code Online (Sandbox Code Playgroud)
一行功能:
function type(obj) {
return Object.prototype.toString.call(obj).replace(/^\[object (.+)\]$/,"$1").toLowerCase()
}
Run Code Online (Sandbox Code Playgroud)
这给出了相同的结果 jQuery.type()
您可以应用于Object.prototype.toString任何对象:
var toString = Object.prototype.toString;
console.log(toString.call([]));
//-> [object Array]
console.log(toString.call(/reg/g));
//-> [object RegExp]
console.log(toString.call({}));
//-> [object Object]
Run Code Online (Sandbox Code Playgroud)
这适用于所有浏览器,IE 除外 - 当在从另一个窗口获得的变量上调用它时,它只会吐出[object Object].