Dan*_*Fox 1194 javascript types object javascript-objects
如何检查值是否是JavaScript中的对象?
Chu*_*uck 1409
如果typeof yourVariable === 'object'
,它是一个对象或null.如果要排除null,只需创建它typeof yourVariable === 'object' && yourVariable !== null
.
Mat*_*ick 516
让我们在Javascript中定义"对象".根据MDN文档,每个值都是对象或原语:
原始的,原始的价值
不是对象且没有任何方法的数据.JavaScript有5种原始数据类型:string,number,boolean,null,undefined.
什么是原始的?
3
'abc'
true
null
undefined
什么是对象(即不是原始对象)?
Object.prototype
Object.prototype
Function.prototype
Object
Function
function C(){}
- 用户定义的功能C.prototype
- 用户定义函数的prototype属性:这不是 C
原型
new C()
- "新" - 用户定义的功能Math
Array.prototype
{"a": 1, "b": 2}
- 使用文字符号创建的对象new Number(3)
- 基元周围的包装Object.create(null)
Object.create(null)
如何检查值是否为对象
instanceof
本身不起作用,因为它错过了两个案例:
// oops: isObject(Object.prototype) -> false
// oops: isObject(Object.create(null)) -> false
function isObject(val) {
return val instanceof Object;
}
Run Code Online (Sandbox Code Playgroud)
typeof x === 'object'
因为误报(null
)和漏报(函数)而无效:
// oops: isObject(Object) -> false
function isObject(val) {
return (typeof val === 'object');
}
Run Code Online (Sandbox Code Playgroud)
Object.prototype.toString.call
由于所有基元的误报,它将无法工作:
> Object.prototype.toString.call(3)
"[object Number]"
> Object.prototype.toString.call(new Number(3))
"[object Number]"
Run Code Online (Sandbox Code Playgroud)
所以我使用:
function isObject(val) {
if (val === null) { return false;}
return ( (typeof val === 'function') || (typeof val === 'object') );
}
Run Code Online (Sandbox Code Playgroud)
@ Daan的答案似乎也有效:
function isObject(obj) {
return obj === Object(obj);
}
Run Code Online (Sandbox Code Playgroud)
因为,根据MDN文档:
Object构造函数为给定值创建一个对象包装器.如果值为null或未定义,则它将创建并返回空对象,否则,它将返回与给定值对应的类型的对象.如果值已经是一个对象,它将返回该值.
似乎有效的第三种方法(不确定它是否为100%)是使用Object.getPrototypeOf
哪种方法如果其参数不是对象则抛出异常:
// these 5 examples throw exceptions
Object.getPrototypeOf(null)
Object.getPrototypeOf(undefined)
Object.getPrototypeOf(3)
Object.getPrototypeOf('abc')
Object.getPrototypeOf(true)
// these 5 examples don't throw exceptions
Object.getPrototypeOf(Object)
Object.getPrototypeOf(Object.prototype)
Object.getPrototypeOf(Object.create(null))
Object.getPrototypeOf([])
Object.getPrototypeOf({})
Run Code Online (Sandbox Code Playgroud)
Mic*_*ker 468
尝试使用null
和/或object
.
编辑:这个答案给出了如何检查变量属性的想法,但它不是一个防弹配方(毕竟根本没有配方!)用于检查它是否是一个对象,远离它.由于人们倾向于在没有进行任何研究的情况下从这里寻找要复制的东西,我强烈建议他们转向另一个,最受欢迎(并且正确!)的答案.
Daa*_*aan 265
官方的underscore.js使用此检查来确定某些东西是否真的是一个对象
// Is a given variable an object?
_.isObject = function(obj) {
return obj === Object(obj);
};
Run Code Online (Sandbox Code Playgroud)
UPDATE
该underscore.js更新库现在是用下面的,因为在V8之前的错误和少量微速度优化.
// Is a given variable an object?
_.isObject = function(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
};
Run Code Online (Sandbox Code Playgroud)
Chr*_*phe 173
Object.prototype.toString.call(myVar)
将返回:
"[object Object]"
如果myVar是一个对象"[object Array]"
如果myVar是一个数组有关这方面的更多信息以及为什么它是typeof的一个很好的替代方案,请查看本文.
zup*_*upa 100
仅用于检查对象或数组而无需额外的函数调用(速度).也发布在这里.
IsArray的()
isArray = function(a) {
return (!!a) && (a.constructor === Array);
};
console.log(isArray( )); // false
console.log(isArray( null)); // false
console.log(isArray( true)); // false
console.log(isArray( 1)); // false
console.log(isArray( 'str')); // false
console.log(isArray( {})); // false
console.log(isArray(new Date)); // false
console.log(isArray( [])); // true
Run Code Online (Sandbox Code Playgroud)
isObject() - 注意:仅用于Object文字,因为它为自定义对象返回false,例如new Date或new YourCustomObject.
isObject = function(a) {
return (!!a) && (a.constructor === Object);
};
console.log(isObject( )); // false
console.log(isObject( null)); // false
console.log(isObject( true)); // false
console.log(isObject( 1)); // false
console.log(isObject( 'str')); // false
console.log(isObject( [])); // false
console.log(isObject(new Date)); // false
console.log(isObject( {})); // true
Run Code Online (Sandbox Code Playgroud)
Vla*_*pak 75
Array.isArray
:function isObject(o) {
return o !== null && typeof o === 'object' && Array.isArray(o) === false;
}
Run Code Online (Sandbox Code Playgroud)
Array.isArray
:只是惊讶了多少赞成错误答案
只有1个答案通过我的测试!在这里,我创建了我的简化版本:
function isObject(o) {
return o instanceof Object && o.constructor === Object;
}
Run Code Online (Sandbox Code Playgroud)
至于我,它清晰简单,只是有效!在这里我的测试:
console.log(isObject({})); // Will return: true
console.log(isObject([])); // Will return: false
console.log(isObject(null)); // Will return: false
console.log(isObject(/.*/)); // Will return: false
console.log(isObject(function () {})); // Will return: false
Run Code Online (Sandbox Code Playgroud)
还有一次:并非所有答案都通过了这个测试!
如果您需要验证该对象是否是特定类的实例,则必须使用您的特定类检查构造函数,例如:
function isDate(o) {
return o instanceof Object && o.constructor === Date;
}
Run Code Online (Sandbox Code Playgroud)
简单测试:
var d = new Date();
console.log(isObject(d)); // Will return: false
console.log(isDate(d)); // Will return: true
Run Code Online (Sandbox Code Playgroud)
因此,您将拥有严格而强大的代码!
如果你不会喜欢创造功能isDate
,isError
,isRegExp
,等你可以考虑选择使用这种广义函数:
function isObject(o) {
return o instanceof Object && typeof o.constructor === 'function';
}
Run Code Online (Sandbox Code Playgroud)
对于前面提到的所有测试用例,它都无法正常工作,但它对所有对象(普通或构造)都足够好.
isObject
如果Object.create(null)
因为内部实现在这里Object.create
解释,但你可以使用isObject
更复杂的实现,将不会工作:
function isObject(o, strict = true) {
if (o === null || o === undefined) {
return false;
}
const instanceOfObject = o instanceof Object;
const typeOfObject = typeof o === 'object';
const constructorUndefined = o.constructor === undefined;
const constructorObject = o.constructor === Object;
const typeOfConstructorObject = typeof o.constructor === 'function';
let r;
if (strict === true) {
r = (instanceOfObject || typeOfObject) && (constructorUndefined || constructorObject);
} else {
r = (constructorUndefined || typeOfConstructorObject);
}
return r;
};
Run Code Online (Sandbox Code Playgroud)
根据此实现,已经在npm v1上创建了包!它适用于所有早期描述的测试用例!
jth*_*ter 74
我很喜欢:
function isObject (item) {
return (typeof item === "object" && !Array.isArray(item) && item !== null);
}
Run Code Online (Sandbox Code Playgroud)
如果该项是JS对象,并且它不是JS数组,并且它不是null
......如果所有三个都证明是真的,则返回true
.如果三个条件中的任何一个失败,&&
测试将短路false
并将返回.在null
如果需要的话(这取决于你如何使用测试可以省略null
).
DOCS:
http://devdocs.io/javascript/operators/typeof
http://devdocs.io/javascript/global_objects/object
http://devdocs.io/javascript/global_objects/array/isarray
http://devdocs.io/javascript/global_objects/null
Eri*_*eni 30
typeof JavaScript构造函数和对象(包括null
)返回"object"
function isObject(obj)
{
return obj != null && obj.constructor.name === "Object"
}
console.log(isObject({})) // returns true
console.log(isObject([])) // returns false
console.log(isObject(null)) // returns false
Run Code Online (Sandbox Code Playgroud)
检查其constructor
属性返回函数及其名称.
console.log(typeof null, typeof [], typeof {})
Run Code Online (Sandbox Code Playgroud)
Function.name
返回函数的只读名称或"anonymous"
闭包.
console.log(({}).constructor) // returns a function with name "Object"
console.log(([]).constructor) // returns a function with name "Array"
console.log((null).constructor) //throws an error because null does not actually have a property
Run Code Online (Sandbox Code Playgroud)
console.log(({}).constructor.name) // returns "Object"
console.log(([]).constructor.name) // returns "Array"
console.log((null).constructor.name) //throws an error because null does not actually have a property
Run Code Online (Sandbox Code Playgroud)
注意: Function.name可能无法在IE中运行 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name#Browser_compatibility
Ali*_*eza 29
OK,让我们给你这个概念首先回答你的问题,在JavaScript函数之前都是对象,也为null,对象,数组,甚至日期,所以你看有没有喜欢的typeof的obj ==="对象"一个简单的方法,所以上面提到的所有内容都将返回true,但有一些方法可以通过编写函数或使用JavaScript框架来检查它,OK:
现在,假设你有这个对象是一个真实的对象(不是null或函数或数组):
var obj = {obj1: 'obj1', obj2: 'obj2'};
Run Code Online (Sandbox Code Playgroud)
纯JavaScript:
//that's how it gets checked in angular framework
function isObject(obj) {
return obj !== null && typeof obj === 'object';
}
Run Code Online (Sandbox Code Playgroud)
要么
//make sure the second object is capitalised
function isObject(obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
}
Run Code Online (Sandbox Code Playgroud)
要么
function isObject(obj) {
return obj.constructor.toString().indexOf("Object") > -1;
}
Run Code Online (Sandbox Code Playgroud)
要么
function isObject(obj) {
return obj instanceof Object;
}
Run Code Online (Sandbox Code Playgroud)
您可以通过调用它们来简单地在代码中使用上述函数之一,如果它是一个对象,它将返回true:
isObject(obj);
Run Code Online (Sandbox Code Playgroud)
如果您使用的是JavaScript框架,他们通常会为您准备这些函数,这些函数很少:
jQuery的:
//It returns 'object' if real Object;
jQuery.type(obj);
Run Code Online (Sandbox Code Playgroud)
角度:
angular.isObject(obj);
Run Code Online (Sandbox Code Playgroud)
下划线和Lodash:
//(NOTE: in Underscore and Lodash, functions, arrays return true as well but not null)
_.isObject(obj);
Run Code Online (Sandbox Code Playgroud)
las*_*ild 23
这取决于你对"是一个对象"的意思.如果你想要一些不是原始的东西,即你可以设置新属性的东西,这应该可以解决问题:
function isAnyObject(value) {
return value != null && (typeof value === 'object' || typeof value === 'function');
}
Run Code Online (Sandbox Code Playgroud)
它不包括原语(纯数字/ NaN
/ Infinity
,普通字符串,符号,true
/ false
,undefined
和null
),但应返回其他一切(包括真正的Number
,Boolean
和String
对象).请注意,JS没有定义"主机"对象(例如window
or console
)应该在使用时返回typeof
,因此很难用这样的支票覆盖.
如果你想知道的东西是否是一个"普通"的对象,也就是说,它是作为文字创建{}
或者Object.create(null)
,你可以这样做:
function isPlainObject(value) {
if (Object.prototype.toString.call(value) !== '[object Object]') {
return false;
} else {
var prototype = Object.getPrototypeOf(value);
return prototype === null || prototype === Object.prototype;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑2018:因为Symbol.toStringTag
现在允许自定义输出Object.prototype.toString.call(...)
,所以在某些情况下,即使对象以文字形式开始生命,上述isPlainObject
函数也可能返回false
.可以说,按照惯例,具有自定义字符串标记的对象不再是一个普通对象,但这进一步混淆了普通对象在Javascript中的定义.
Ori*_*iol 17
检查值类型的最合理方法似乎是typeof
运算符.唯一的问题是它可怕的破碎:
"object"
的null
,属于null类型."function"
属于Object类型的可调用对象."unknown"
.唯一禁止的结果是"function"
原始类型.typeof
对于非null
原语只是可靠的.因此,检查值是否为对象的方法是确保返回的字符串typeof
不对应于基元,而对象则不对应null
.然而,问题是,未来的标准可能会引入新的基本类型,我们的代码会认为它是一个对象.新类型不会频繁出现,但例如ECMAScript 6引入了Symbol类型.
因此,typeof
我只推荐其结果因值是否为对象而变化的方法,而不是.以下打算成为一个
Object
构造函数
该Object
构造胁迫传递的参数的对象.如果它已经是对象,则返回相同的对象.
因此,您可以使用它来强制对象的值,并严格地将该对象与原始值进行比较.
以下函数需要ECMAScript 3,它引入了===
:
function isObject(value) { /* Requires ECMAScript 3 or later */
return Object(value) === value;
}
Run Code Online (Sandbox Code Playgroud)
我喜欢这种方法,因为它简单且具有自我描述性,类似的检查也适用于布尔值,数字和字符串.但是,请注意它依赖于全局Object
不被遮蔽或改变.
构造函数
实例化构造函数时,它可以返回与刚创建的实例不同的值.但除非它是一个对象,否则该值将被忽略.
以下函数需要ECMAScript 3,它允许构造函数返回非对象.在ECMAScript 3之前发生了错误,但try
当时的语句不存在.
function isObject(value) { /* Requires ECMAScript 3 or later */
return new function() { return value; }() === value;
}
Run Code Online (Sandbox Code Playgroud)
虽然比前一个例子简单一点,但这个不依赖于任何全局属性,因此可能是最安全的.
this
值
旧的ECMAScript规范要求该this
值为对象.引入了ECMAScript 3 Function.prototype.call
,它允许调用具有任意this
值的函数,但强制转换为对象.
ECMAScript 5引入了一种严格的模式来消除这种行为,但是在草率模式下我们仍然可以(但可以说不应该)依赖它.
function isObject(value) { /* Requires ECMAScript 3 or later in sloppy mode */
return function() { return this === value; }.call(value);
}
Run Code Online (Sandbox Code Playgroud)[[原型]]
所有普通对象都有一个名为[[Prototype]]的内部插槽,其值决定了它从哪个其他对象继承.该值只能是对象或null
.因此,您可以尝试创建一个继承所需值的对象,并检查它是否有效.
无论Object.create
和Object.getPrototypeOf
需要的ECMAScript 5.
function isObject(value) { /* Requires ECMAScript 5 or later */
try {
Object.create(value);
return value !== null;
} catch(err) {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
function isObject(value) { /* Requires ECMAScript 5 or later */
function Constructor() {}
Constructor.prototype = value;
return Object.getPrototypeOf(new Constructor()) === value;
}
Run Code Online (Sandbox Code Playgroud)一些新的ECMAScript 6方式
ECMAScript 6引入了一些新的间接方法来检查一个值是一个对象.他们使用先前看到的方法将值传递给需要对象的一些代码,这些代码包含在try
语句中以捕获错误.一些隐藏的例子,不值得评论
function isObject(value) { /* Requires ECMAScript 6 or later */
try {
Object.setPrototypeOf({}, value);
return value !== null;
} catch(err) {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
function isObject(value) { /* Requires ECMAScript 6 or later */
try {
new WeakSet([value]);
return true;
} catch(err) {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
注意:我故意跳过一些方法,如Object.getPrototypeOf(value)
(ES5)和Reflect
方法(ES6),因为它们调用了必要的内部方法,这可能会产生令人讨厌的事情,例如,如果value
是代理.出于安全原因,我的示例仅参考value
而不直接访问它.
kus*_*lvm 17
天哪,其他答案太混乱了.
简答
typeof anyVar == 'object' && anyVar instanceof Object && !(anyVar instanceof Array)
要测试它,只需在chrome控制台中运行以下语句.
情况1.
var anyVar = {};
typeof anyVar == 'object' && anyVar instanceof Object && !(anyVar instanceof Array) // true
Run Code Online (Sandbox Code Playgroud)
案例2.
anyVar = [];
typeof anyVar == 'object' && anyVar instanceof Object && !(anyVar instanceof Array) // false
Run Code Online (Sandbox Code Playgroud)
案例3.
anyVar = null;
typeof anyVar == 'object' && anyVar instanceof Object && !(anyVar instanceof Array); // false
Run Code Online (Sandbox Code Playgroud)
说明
好吧.让我们分手吧
typeof anyVar == 'object'
从三位候选人那里得到回报 - [], {} and null
,
anyVar instanceof Object
将这些候选人缩小到两个 - [], {}
!(anyVar instanceof Array)
缩小到只有一个 - {}
请滚筒!
通过这个你可能已经学会了如何在Javascript中检查数组.
Tal*_*lha 16
试试这个
if (objectName instanceof Object == false) {
alert('Not an object');
}
else {
alert('An object');
}
Run Code Online (Sandbox Code Playgroud)
Jay*_*wal 15
这是一个带有可选链接的答案,也许是isObj
这个问题的最小函数。
const isObj = o => o?.constructor === Object;
// True for this
console.log(isObj({})); // object!
// False for these
console.log(isObj(0)); // number
console.log(isObj([])); // array
console.log(isObj('lol')); // string
console.log(isObj(null)); // null
console.log(isObj(undefined)); // undefined
console.log(isObj(() => {})); // function
console.log(isObj(Object)); // class
Run Code Online (Sandbox Code Playgroud)
Kan*_*nia 12
var a = [1]
typeof a //"object"
a instanceof Object //true
a instanceof Array //true
var b ={a: 1}
b instanceof Object //true
b instanceof Array //false
var c = null
c instanceof Object //false
c instanceof Array //false
Run Code Online (Sandbox Code Playgroud)
我被要求提供更多细节.检查我们的变量是否是对象的最干净和可理解的方法是typeof myVar
.它返回一个字符串类型(例如"object"
,"undefined"
).
不幸的是,Array和null也有一个类型object
.要仅使用真实对象,需要使用instanceof
运算符检查继承链.它将消除null,但Array在继承链中具有Object.
所以解决方案是:
if (myVar instanceof Object && !(myVar instanceof Array)) {
// code for objects
}
Run Code Online (Sandbox Code Playgroud)
Ina*_*mus 12
function isObject(o) {
return null != o &&
typeof o === 'object' &&
Object.prototype.toString.call(o) === '[object Object]';
}
function isDerivedObject(o) {
return !isObject(o) &&
null != o &&
(typeof o === 'object' || typeof o === 'function') &&
/^\[object /.test(Object.prototype.toString.call(o));
}
// Loose equality operator (==) is intentionally used to check
// for undefined too
// Also note that, even null is an object, within isDerivedObject
// function we skip that and always return false for null
Run Code Online (Sandbox Code Playgroud)
在Javascript中,null
,Object
,Array
,Date
和function
s为所有对象.虽然,null
有点做作.因此,最好检查第null
一个,检测它不是空的.
检查作为对象的typeof o === 'object'
保证o
.如果没有这个检查,那Object.prototype.toString
将毫无意义,因为它会返回对象,即使是undefined
和null
!例如:toString(undefined)
退货[object Undefined]
!
后typeof o === 'object'
检查,toString.call(O)是检查是否一个伟大的方法o
是一个对象,一个派生对象像Array
,Date
或一个function
.
在isDerivedObject
功能中,它检查o
是一个功能.因为,功能也是一个对象,这就是为什么它在那里.如果没有这样做,函数将返回false.示例:isDerivedObject(function() {})
将返回false
,但现在它返回true
.
人们总是可以改变对象的定义.因此,可以相应地改变这些功能.
function isObject(o) {
return null != o &&
typeof o === 'object' &&
Object.prototype.toString.call(o) === '[object Object]';
}
function isDerivedObject(o) {
return !isObject(o) &&
null != o &&
(typeof o === 'object' || typeof o === 'function') &&
/^\[object /.test(Object.prototype.toString.call(o));
}
// TESTS
// is null an object?
console.log(
'is null an object?', isObject(null)
);
console.log(
'is null a derived object?', isDerivedObject(null)
);
// is 1234 an object?
console.log(
'is 1234 an object?', isObject(1234)
);
console.log(
'is 1234 a derived object?', isDerivedObject(1234)
);
// is new Number(1234) an object?
console.log(
'is new Number(1234) an object?', isObject(new Number(1234))
);
console.log(
'is new Number(1234) a derived object?', isDerivedObject(1234)
);
// is function object an object?
console.log(
'is (new (function (){})) an object?',
isObject((new (function (){})))
);
console.log(
'is (new (function (){})) a derived object?',
isObject((new (function (){})))
);
// is {} an object?
console.log(
'is {} an object?', isObject({})
);
console.log(
'is {} a derived object?', isDerivedObject({})
);
// is Array an object?
console.log(
'is Array an object?',
isObject([])
)
console.log(
'is Array a derived object?',
isDerivedObject([])
)
// is Date an object?
console.log(
'is Date an object?', isObject(new Date())
);
console.log(
'is Date a derived object?', isDerivedObject(new Date())
);
// is function an object?
console.log(
'is function an object?', isObject(function(){})
);
console.log(
'is function a derived object?', isDerivedObject(function(){})
);
Run Code Online (Sandbox Code Playgroud)
Emi*_*lía 11
很晚才......对于"普通物体"(我的意思是,像{'x':5,'y':7})我有这个小片段:
function isPlainObject(o) {
return ((o === null) || Array.isArray(o) || typeof o == 'function') ?
false
:(typeof o == 'object');
}
Run Code Online (Sandbox Code Playgroud)
它会生成下一个输出:
console.debug(isPlainObject(isPlainObject)); //function, false
console.debug(isPlainObject({'x': 6, 'y': 16})); //literal object, true
console.debug(isPlainObject(5)); //number, false
console.debug(isPlainObject(undefined)); //undefined, false
console.debug(isPlainObject(null)); //null, false
console.debug(isPlainObject('a')); //string, false
console.debug(isPlainObject([])); //array?, false
console.debug(isPlainObject(true)); //bool, false
console.debug(isPlainObject(false)); //bool, false
Run Code Online (Sandbox Code Playgroud)
它总是适合我.如果"o"的类型是"object"但是没有null,数组或函数,则返回"true".:)
由于关于如何正确处理这个问题似乎有很多困惑,我将留下2美分(这个答案符合规范并在所有情况下产生正确的结果):
测试原语:
undefined
null
boolean
string
number
function isPrimitive(o){return typeof o!=='object'||null}
Run Code Online (Sandbox Code Playgroud)
对象不是原始的:
function isObject(o){return !isPrimitive(o)}
Run Code Online (Sandbox Code Playgroud)
或者:
function isObject(o){return o instanceof Object}
function isPrimitive(o){return !isObject(o)}
Run Code Online (Sandbox Code Playgroud)
测试任何数组:
const isArray=(function(){
const arrayTypes=Object.create(null);
arrayTypes['Array']=true;
arrayTypes['Int8Array']=true;
arrayTypes['Uint8Array']=true;
arrayTypes['Uint8ClampedArray']=true;
arrayTypes['Int16Array']=true;
arrayTypes['Uint16Array']=true;
arrayTypes['Int32Array']=true;
arrayTypes['Uint32Array']=true;
arrayTypes['BigInt64Array']=true;
arrayTypes['BigUint64Array']=true;
arrayTypes['Float32Array']=true;
arrayTypes['Float64Array']=true;
return function(o){
if (!o) return false;
return !isPrimitive(o)&&!!arrayTypes[o.constructor.name];
}
}());
Run Code Online (Sandbox Code Playgroud)
测试对象排除:Date
RegExp
Boolean
Number
String
Function
任何数组
const isObjectStrict=(function(){
const nativeTypes=Object.create(null);
nativeTypes['Date']=true;
nativeTypes['RegExp']=true;
nativeTypes['Boolean']=true;
nativeTypes['Number']=true;
nativeTypes['String']=true;
nativeTypes['Function']=true;
return function(o){
if (!o) return false;
return !isPrimitive(o)&&!isArray(o)&&!nativeTypes[o.constructor.name];
}
}());
Run Code Online (Sandbox Code Playgroud)
小智 8
当其他一切都失败时,我用这个:
var isObject = function(item) {
return item.constructor.name === "Object";
};
Run Code Online (Sandbox Code Playgroud)
这会奏效.它是一个返回true,false或null的函数.
const isObject = obj => obj && obj.constructor && obj.constructor === Object;
console.log(isObject({})); // true
console.log(isObject([])); // false
console.log(isObject(new Function)); // false
console.log(isObject(new Number(123))); // false
console.log(isObject(null)); // null
Run Code Online (Sandbox Code Playgroud)
为了我的代码的目的,我发现这个决定与上面的一些答案相对应:
ES6 变体:
const checkType = o => Object.prototype
.toString
.call(o)
.replace(/\[|object\s|\]/g, '')
.toLowerCase();
Run Code Online (Sandbox Code Playgroud)
ES5 变体:
function checkType(o){
return Object.prototype
.toString
.call(o)
.replace(/\[|object\s|\]/g, '')
.toLowerCase();
}
Run Code Online (Sandbox Code Playgroud)
您可以非常简单地使用它:
checkType([]) === 'array'; // true
checkType({}) === 'object'; // true
checkType(1) === 'number'; // true
checkType('') === 'string'; // true
checkType({}.p) === 'undefined'; // true
checkType(null) === 'null'; // true
Run Code Online (Sandbox Code Playgroud)
等等..
if(typeof value === 'object' && value.constructor === Object)
{
console.log("This is an object");
}
Run Code Online (Sandbox Code Playgroud)
所述Ramda功能库具有用于检测JavaScript类型中一个精彩功能.
解释全部功能:
function type(val) {
return val === null ? 'Null' :
val === undefined ? 'Undefined' :
Object.prototype.toString.call(val).slice(8, -1);
}
Run Code Online (Sandbox Code Playgroud)
当我意识到解决方案的简单和美观时,我不得不笑.
Ramda 文档的示例用法:
R.type({}); //=> "Object"
R.type(1); //=> "Number"
R.type(false); //=> "Boolean"
R.type('s'); //=> "String"
R.type(null); //=> "Null"
R.type([]); //=> "Array"
R.type(/[A-z]/); //=> "RegExp"
R.type(() => {}); //=> "Function"
R.type(undefined); //=> "Undefined"
Run Code Online (Sandbox Code Playgroud)
如果您要检查prototype
for的object
唯一来源Object
。过滤掉String
,Number
,Array
,Arguments
,等。
function isObject (n) {
return Object.prototype.toString.call(n) === '[object Object]';
}
Run Code Online (Sandbox Code Playgroud)
或作为单表达式箭头功能(ES6 +)
const isObject = n => Object.prototype.toString.call(n) === '[object Object]'
Run Code Online (Sandbox Code Playgroud)
阅读和尝试了很多的实现之后,我发现很少有人尝试检查类似值JSON
,Math
,document
再与原型链或物体比1步。
typeof
我认为最好是将检查保持尽可能简单,以免在添加新的原始对象或本机对象注册为typeof
“对象” 时进行重构,而不是检查变量的大小写然后消除边缘情况,这会更好。'。
毕竟,typeof
操作员会告诉您某个对象是否是JavaScript的对象,但是JavaScript的对象定义对于大多数实际场景(例如typeof null === 'object'
)而言太宽泛了。下面是一个函数,该函数v
通过本质上重复两次检查来确定变量是否为对象:
v
是'[object Object]'
。v
被替换为链中的下一个原型v = Object.getPrototypeOf(v)
,但之后也直接进行评估。当新的值为v
is时null
,这意味着包括根原型(很可能是链中唯一的原型)在内的每个原型都已通过while循环中的检查,我们可以返回true。否则,将开始新的迭代。function isObj (v) {
while ( Object.prototype.toString.call(v) === '[object Object]')
if ((v = Object.getPrototypeOf(v)) === null)
return true
return false
}
console.log('FALSE:')
console.log('[] -> ', isObj([]))
console.log('null -> ', isObj(null))
console.log('document -> ', isObj(document))
console.log('JSON -> ', isObj(JSON))
console.log('function -> ', isObj(function () {}))
console.log('new Date() -> ', isObj(new Date()))
console.log('RegExp -> ', isObj(/./))
console.log('TRUE:')
console.log('{} -> ', isObj({}))
console.log('new Object() -> ', isObj(new Object()))
console.log('new Object(null) -> ', isObj(new Object(null)))
console.log('new Object({}) -> ', isObj(new Object({foo: 'bar'})))
console.log('Object.prototype -> ', isObj(Object.prototype))
console.log('Object.create(null) -> ', isObj(Object.create(null)))
console.log('Object.create({}) -> ', isObj(Object.create({foo: 'bar'})))
console.log('deep inheritance -> ', isObj(Object.create(Object.create({foo: 'bar'}))))
Run Code Online (Sandbox Code Playgroud)
这是一个老问题,但想把它留在这里。大多数人正在检查变量是否{}
表示键值对,而不是 JavaScript 用于给定事物的下划线构造是什么,因为老实说,JavaScript 中的大部分内容都是对象。所以把它排除在外。如果你这样做...
let x = function() {}
typeof x === 'function' //true
x === Object(x) // true
x = []
x === Object(x) // true
// also
x = null
typeof null // 'object'
Run Code Online (Sandbox Code Playgroud)
大多数时候,我们想要知道我们是否有来自 API 的资源对象或从 ORM 返回的数据库调用。然后我们可以测试是否不是 an Array
,不是null
,不是 typeof 'function'
,并且是Object
// To account also for new Date() as @toddmo pointed out
x instanceof Object && x.constructor === Object
x = 'test' // false
x = 3 // false
x = 45.6 // false
x = undefiend // false
x = 'undefiend' // false
x = null // false
x = function(){} // false
x = [1, 2] // false
x = new Date() // false
x = {} // true
Run Code Online (Sandbox Code Playgroud)
今天 2020.09.26 我在 Chrome v85、Safari v13.1.2 和 Firefox v80 上对 MacOs HighSierra 10.13.6 进行测试,以选择解决方案。
我为解决方案执行了 3 个测试用例 A B C D E F G H I J K L M N O P Q R S T U V
下面的代码片段展示了解决方案之间的差异。Solutions AG 为Matt Fenwick描述的选定案例提供正确答案
// https://stackoverflow.com/a/14706877/860099
function A(x) {
return x === Object(x);
};
// https://stackoverflow.com/a/42250981/860099
function B(x) {
return _.isObject(x);
}
// https://stackoverflow.com/a/34864175/860099
function C(x) {
return x != null && (typeof x === 'object' || typeof x === 'function');
}
// https://stackoverflow.com/a/39187058/860099
function D(x) {
return new function() { return x; }() === x;
}
// https://stackoverflow.com/a/39187058/860099
function E(x) {
return function() { return this === x; }.call(x);
}
// https://stackoverflow.com/a/39187058/860099
function F(x) { /* Requires ECMAScript 5 or later */
try {
Object.create(x);
return x !== null;
} catch(err) {
return false;
}
}
// https://stackoverflow.com/a/39187058/860099
function G(x) { /* Requires ECMAScript 5 or later */
function Constructor() {}
Constructor.prototype = x;
return Object.getPrototypeOf(new Constructor()) === x;
}
// https://stackoverflow.com/a/8511332/860099
function H(x) {
return typeof x === 'object' && x !== null
}
// https://stackoverflow.com/a/25715455/860099
function I(x) {
return (typeof x === "object" && !Array.isArray(x) && x !== null);
};
// https://stackoverflow.com/a/22482737/860099
function J(x) {
return x instanceof Object;
}
// https://stackoverflow.com/a/50712057/860099
function K(x)
{
let t= JSON.stringify(x);
return t ? t[0] === '{' : false;
}
// https://stackoverflow.com/a/13356338/860099
function L(x) {
return Object.prototype.toString.call(x) === "[object Object]";
};
// https://stackoverflow.com/a/46663081/860099
function M(o, strict = true) {
if (o === null || o === undefined) {
return false;
}
const instanceOfObject = o instanceof Object;
const typeOfObject = typeof o === 'object';
const constructorUndefined = o.constructor === undefined;
const constructorObject = o.constructor === Object;
const typeOfConstructorObject = typeof o.constructor === 'function';
let r;
if (strict === true) {
r = (instanceOfObject || typeOfObject) && (constructorUndefined || constructorObject);
} else {
r = (constructorUndefined || typeOfConstructorObject);
}
return r;
}
// https://stackoverflow.com/a/42250981/860099
function N(x) {
return $.type(x) === 'object';
}
// https://stackoverflow.com/a/34864175/860099
function O(x) {
if (Object.prototype.toString.call(x) !== '[object Object]') {
return false;
} else {
var prototype = Object.getPrototypeOf(x);
return prototype === null || prototype === Object.prototype;
}
}
// https://stackoverflow.com/a/57863169/860099
function P(x) {
while ( Object.prototype.toString.call(x) === '[object Object]')
if ((x = Object.getPrototypeOf(x)) === null)
return true
return false
}
// https://stackoverflow.com/a/43289971/860099
function Q(x){
try{
switch(x.constructor){
case Number:
case Function:
case Boolean:
case Symbol:
case Date:
case String:
case RegExp:
return x.constructor === Object;
case Error:
case EvalError:
case RangeError:
case ReferenceError:
case SyntaxError:
case TypeError:
case URIError:
return (Object === Error ? Error : x.constructor) === Object;
case Array:
case Int8Array:
case Uint8Array:
case Uint8ClampedArray:
case Int16Array:
case Uint16Array:
case Int32Array:
case Uint32Array:
case Float32Array:
case Float64Array:
return (Object === Array ? Array : x.constructor) === Object;
case Object:
default:
return (Object === Object ? Object : x.constructor) === Object;
}
} catch(ex){
return x == Object;
}
}
// https://stackoverflow.com/a/52478680/860099
function R(x) {
return typeof x == 'object' && x instanceof Object && !(x instanceof Array);
}
// https://stackoverflow.com/a/51458052/860099
function S(x)
{
return x != null && x.constructor?.name === "Object"
}
// https://stackoverflow.com/a/42250981/860099
function T(x) {
return x?.constructor?.toString().indexOf("Object") > -1;
}
// https://stackoverflow.com/a/43223661/860099
function U(x)
{
return x?.constructor === Object;
}
// https://stackoverflow.com/a/46663081/860099
function V(x) {
return x instanceof Object && x.constructor === Object;
}
// -------------
// TEST
// -------------
console.log('column: 1 2 3 4 5 6 - 7 8 9 10 11');
[A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V]
.map(f=> console.log(`${f.name}: ${1*f(new Date())} ${1*f(/./)} ${1*f({})} ${1*f(Object.prototype)} ${1*f(Object.create(null))} ${1*f(()=>{})} - ${1*f("abc")} ${1*f(3)} ${1*f(true)} ${1*f(null)} ${1*f(undefined)}`))
console.log(`
Columns legend (test cases):
1: new Date()
2: /./ (RegExp)
3: {}
4: Object.prototype
5: Object.create(null)
6: ()=>{} (function)
7: "abc" (string)
8: 3 (number)
9: true (boolean)
10: null
11: undefined
Rows:
1 = is object
0 = is NOT object
Theoretically columns 1-6 should have have 1, columns 7-11 shoud have 0
`);
Run Code Online (Sandbox Code Playgroud)
<script
src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
crossorigin="anonymous"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"
integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww=="
crossorigin="anonymous"></script>
This shippet only presents functions used in performance tests - it not perform tests itself!
Run Code Online (Sandbox Code Playgroud)
以下是 chrome 的示例结果
var isObject = function(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
};
Run Code Online (Sandbox Code Playgroud)
!!obj
是检查对象是否真实的简写(过滤掉null/undefined)
如果明确要检查给定的值是否为{}
.
function isObject (value) {
return value && typeof value === 'object' && value.constructor === Object;
}
Run Code Online (Sandbox Code Playgroud)
function isObjectLike(value) {
return value != null && typeof value == 'object' && !Array.isArray(value);
}
Run Code Online (Sandbox Code Playgroud)
基于lodash
我从这个 SO 问题中找到了一种“新”方法来进行这种类型检查:为什么 instanceof 会为某些文字返回 false?
由此,我创建了一个用于类型检查的函数,如下所示:
function isVarTypeOf(_var, _type){
try {
return _var.constructor === _type;
} catch(ex) {
return false; //fallback for null or undefined
}
}
Run Code Online (Sandbox Code Playgroud)
那么你可以这样做:
console.log(isVarTypeOf('asdf', String)); // returns true
console.log(isVarTypeOf(new String('asdf'), String)); // returns true
console.log(isVarTypeOf(123, String)); // returns false
console.log(isVarTypeOf(123, Number)); // returns true
console.log(isVarTypeOf(new Date(), String)); // returns false
console.log(isVarTypeOf(new Date(), Number)); // returns false
console.log(isVarTypeOf(new Date(), Date)); // returns true
console.log(isVarTypeOf([], Object)); // returns false
console.log(isVarTypeOf([], Array)); // returns true
console.log(isVarTypeOf({}, Object)); // returns true
console.log(isVarTypeOf({}, Array)); // returns false
console.log(isVarTypeOf(null, Object)); // returns false
console.log(isVarTypeOf(undefined, Object)); // returns false
console.log(isVarTypeOf(false, Boolean)); // returns true
Run Code Online (Sandbox Code Playgroud)
已在 Chrome 56、Firefox 52、Microsoft Edge 38、Internet Explorer 11、Opera 43 上进行测试
编辑:
如果您还想检查变量是否为空或未定义,则可以改用它:
function isVarTypeOf(_var, _type){
try {
return _var.constructor === _type;
} catch(ex) {
return _var == _type; //null and undefined are considered the same
// or you can use === if you want to differentiate them
}
}
var a = undefined, b = null;
console.log(isVarTypeOf(a, undefined)) // returns true
console.log(isVarTypeOf(b, undefined)) // returns true
console.log(isVarTypeOf(a, null)) // returns true
Run Code Online (Sandbox Code Playgroud)
从 inanc 的评论更新:接受挑战:D
如果你想松散比较对象,你可以尝试这种方式:
function isVarTypeOf(_var, _type, looseCompare){
if (!looseCompare){
try {
return _var.constructor === _type;
} catch(ex){
return _var == _type;
}
} else {
try{
switch(_var.constructor){
case Number:
case Function:
case Boolean:
case Symbol:
case Date:
case String:
case RegExp:
// add all standard objects you want to differentiate here
return _var.constructor === _type;
case Error:
case EvalError:
case RangeError:
case ReferenceError:
case SyntaxError:
case TypeError:
case URIError:
// all errors are considered the same when compared to generic Error
return (_type === Error ? Error : _var.constructor) === _type;
case Array:
case Int8Array:
case Uint8Array:
case Uint8ClampedArray:
case Int16Array:
case Uint16Array:
case Int32Array:
case Uint32Array:
case Float32Array:
case Float64Array:
// all types of array are considered the same when compared to generic Array
return (_type === Array ? Array : _var.constructor) === _type;
case Object:
default:
// the remaining are considered as custom class/object, so treat it as object when compared to generic Object
return (_type === Object ? Object : _var.constructor) === _type;
}
} catch(ex){
return _var == _type; //null and undefined are considered the same
// or you can use === if you want to differentiate them
}
}
}
Run Code Online (Sandbox Code Playgroud)
这样,你可以像 inanc 的评论一样:
isVarTypeOf(new (function Foo(){}), Object); // returns false
isVarTypeOf(new (function Foo(){}), Object, true); // returns true
Run Code Online (Sandbox Code Playgroud)
或者
Foo = function(){};
Bar = function(){};
isVarTypeOf(new Foo(), Object); // returns false
isVarTypeOf(new Foo(), Object, true); // returns true
isVarTypeOf(new Bar(), Foo, true); // returns false
isVarTypeOf(new Bar(), Bar, true); // returns true
isVarTypeOf(new Bar(), Bar); // returns true
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1015451 次 |
最近记录: |