该typeof
运营商并没有真正帮助我们找到真正的类的一个对象。
我已经看过以下代码:
Object.prototype.toString.apply(t)
题:
这是检查对象类型的最准确方法吗?
JavaScript规范提供了一种确定对象类的正确方法:
Object.prototype.toString.call(t);
http://bonsaiden.github.com/JavaScript-Garden/#types
这Object.prototype.toString
是一个好方法,但是其性能却是最差的。
http://jsperf.com/check-js-type
使用typeof
解决一些基本问题(字符串,数字,布尔...)和使用Object.prototype.toString
来解决复杂的东西(如数组,日期,正则表达式)。
这是我的解决方案:
var type = (function(global) {
var cache = {};
return function(obj) {
var key;
return obj === null ? 'null' // null
: obj === global ? 'global' // window in browser or global in nodejs
: (key = typeof obj) !== 'object' ? key // basic: string, boolean, number, undefined, function
: obj.nodeType ? 'object' // DOM element
: cache[key = ({}).toString.call(obj)] // cached. date, regexp, error, object, array, math
|| (cache[key] = key.slice(8, -1).toLowerCase()); // get XXXX from [object XXXX], and cache it
};
}(this));
用于:
type(function(){}); // -> "function"
type([1, 2, 3]); // -> "array"
type(new Date()); // -> "date"
type({}); // -> "object"
接受的答案是正确的,但是我喜欢在我构建的大多数项目中定义这个小工具。
var types = {
'get': function(prop) {
return Object.prototype.toString.call(prop);
},
'null': '[object Null]',
'object': '[object Object]',
'array': '[object Array]',
'string': '[object String]',
'boolean': '[object Boolean]',
'number': '[object Number]',
'date': '[object Date]',
}
像这样使用:
if(types.get(prop) == types.number) {
}
如果您使用的是角度,甚至可以将其干净地注入:
angular.constant('types', types);
var o = ...
var proto = Object.getPrototypeOf(o);
proto === SomeThing;
保持您希望对象具有的原型的手柄,然后与之进行比较。
例如
var o = "someString";
var proto = Object.getPrototypeOf(o);
proto === String.prototype; // true
我认为这里显示的大多数解决方案都过度设计。检查值是否为类型的最简单方法可能[object Object]
是检查其.constructor
属性:
function isObject (a) { return a != null && a.constructor === Object; }
甚至使用箭头功能更短:
const isObject = a => a != null && a.constructor === Object;
该a != null
部分是必需的,因为可能会传入null
或,undefined
并且您不能从这两个中的任何一个提取构造函数属性。
它适用于通过以下方式创建的任何对象:
- 该
Object
构造 - 文字
{}
它的另一个简洁功能是它能够为使用的自定义类提供正确的报告Symbol.toStringTag
。例如:
class MimicObject {
get [Symbol.toStringTag]() {
return 'Object';
}
}
这里的问题是,当调用Object.prototype.toString
它的一个实例时,[object Object]
将返回错误的报告:
let fakeObj = new MimicObject();
Object.prototype.toString.call(fakeObj); // -> [object Object]
但是检查构造函数会得出正确的结果:
let fakeObj = new MimicObject();
fakeObj.constructor === Object; // -> false
找出对象的REAL类型(包括原始Object或DataType名称(例如String,Date,Number,.. etc)和对象的REAL类型(甚至是自定义对象)的最佳方法是通过抓取对象原型的构造函数的name属性:
本机类型Ex1:
var string1 = "Test";
console.log(string1.__proto__.constructor.name);
显示:
String
示例2:
var array1 = [];
console.log(array1.__proto__.constructor.name);
显示:
Array
自定义类:
function CustomClass(){
console.log("Custom Class Object Created!");
}
var custom1 = new CustomClass();
console.log(custom1.__proto__.constructor.name);
显示:
CustomClass
我知道的老问题。您不需要转换它。看到这个功能:
function getType( oObj )
{
if( typeof oObj === "object" )
{
return ( oObj === null )?'Null':
// Check if it is an alien object, for example created as {world:'hello'}
( typeof oObj.constructor !== "function" )?'Object':
// else return object name (string)
oObj.constructor.name;
}
// Test simple types (not constructed types)
return ( typeof oObj === "boolean")?'Boolean':
( typeof oObj === "number")?'Number':
( typeof oObj === "string")?'String':
( typeof oObj === "function")?'Function':false;
};
例子:
function MyObject() {}; // Just for example
console.log( getType( new String( "hello ") )); // String
console.log( getType( new Function() ); // Function
console.log( getType( {} )); // Object
console.log( getType( [] )); // Array
console.log( getType( new MyObject() )); // MyObject
var bTest = false,
uAny, // Is undefined
fTest function() {};
// Non constructed standard types
console.log( getType( bTest )); // Boolean
console.log( getType( 1.00 )); // Number
console.log( getType( 2000 )); // Number
console.log( getType( 'hello' )); // String
console.log( getType( "hello" )); // String
console.log( getType( fTest )); // Function
console.log( getType( uAny )); // false, cannot produce
// a string
成本低廉,简单。
该最佳的解决方案是toString
(如上所述):
function getRealObjectType(obj: {}): string {
return Object.prototype.toString.call(obj).match(/\[\w+ (\w+)\]/)[1].toLowerCase();
}
公平警告: toString
考虑为NaN
a,number
因此您稍后必须使用手动进行保护Number.isNaN(value)
。
建议使用另一种解决方案,对和使用Object.getPrototypeOf
失败null
undefined
我整理了一个受上述正确答案启发的类型检查实用程序:
thetypeof = function(name) {
let obj = {};
obj.object = 'object Object'
obj.array = 'object Array'
obj.string = 'object String'
obj.boolean = 'object Boolean'
obj.number = 'object Number'
obj.type = Object.prototype.toString.call(name).slice(1, -1)
obj.name = Object.prototype.toString.call(name).slice(8, -1)
obj.is = (ofType) => {
ofType = ofType.toLowerCase();
return (obj.type === obj[ofType])? true: false
}
obj.isnt = (ofType) => {
ofType = ofType.toLowerCase();
return (obj.type !== obj[ofType])? true: false
}
obj.error = (ofType) => {
throw new TypeError(`The type of ${name} is ${obj.name}: `
+`it should be of type ${ofType}`)
}
return obj;
};
例:
if (thetypeof(prop).isnt('String')) thetypeof(prop).error('String')
if (thetypeof(prop).is('Number')) // do something
文章标签:javascript
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!