为什么JavaScript中没有内置方法来检查对象是否是普通对象? [英] Why is there not a built-in method in JavaScript to check if an object is a plain object?

查看:122
本文介绍了为什么JavaScript中没有内置方法来检查对象是否是普通对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于语言开始以来 JavaScript 的发展,为什么没有内置方法来检查对象是否是普通对象?

Given the developments of JavaScript since the languages' inception, why is there not a built in method that checks if an object is a plain object?

或者该方法实际存在吗?

Or does the method in fact exist?

推荐答案

没有任何明确的直接检查值是否为对象的方法,即属于对象类型,但有一些万无一失的方法可以做到这一点。 我在另一个答案中写了一个列表,最简洁似乎

There doesn't exist any explicit direct way to check if a value is an object, i.e. belongs to Object type, but there are some foolproof ways to do it. I wrote a list in another answer, the most succinct seems

function isObject(value) {
  return Object(value) === value;
}

在es escucuss上已多次请求此类功能。例如,

A feature like this has been requested multiple times on esdiscuss. For example,

Juriy Zaytsevkangax想知道一个正确的方法检查某个值是否为对象。

Juriy Zaytsev "kangax" wonders about a proper way to check if a value is an object.

typeof null

  • Brendan Eich: "I think we should consider Object.isObject"
  • Jorge: "Why not .isPrimitive()?"

ES6不需要选择-in

  • Brendan Eich: "We want sane isObject and isNull predicates"
  • Axel Rauschmayer: "predicates such as isObject() and isPrimitive()"

实际上, Object.isObject 被提议为稻草人它出现在ES6早期草案中


  • TC39抨击:讨论<$ ES6草案中的c $ c> Object.isObject 。

Sym有多原始波士?大数?等:讨论 x ===对象(x)

Object.isObject strawman最终被拒绝并从ES6草案中删除。

Object.isObject strawman was eventually rejected and removed from ES6 draft.

最近,

  • ES8 Proposal: Optional Static Typing (Brandon Andrews): Includes Object.isObject

现在有 {Type}方法第0阶段提案,其中包括 Object.isObject ,其中包括许多其他支票。

Now there is the is{Type} Methods stage 0 proposal which includes Object.isObject among lots of various other checks.

所以仍有希望,最终我们可能有这样的事情。

So there is still hope and eventually we may have something like this.

以上是一般的测试对象。如果你不想要你应该定义普通对象对你来说意味着什么。

The above is for testing objects in general. If you don't want that you should define what "plain object" means for you.

例如,你可以测试构造函数财产。但是任何对象都可以自定义它。

For example, you can test the constructor property. But any object can customize it.

您可以使用 Object.prototype.toString 来获取旧版ES5 [[类]]。但任何对象都可以通过 Symbol.toStringTag 自定义。

You can use Object.prototype.toString to get the legacy ES5 [[Class]]. But any object can customize that via Symbol.toStringTag.

您可以检查[[GetPrototypeOf]返回的值]。但即使是异国情调的对象也可能允许将原型更改为任意对象或null。而Proxy对象甚至可以完全控制该内部方法。

You can check the value returned by [[GetPrototypeOf]]. But even exotic objects might allow their prototype to be changed to whatever arbitrary object or null. And Proxy objects even have full control over that internal method.

所以很可能你不能依赖这些测试。并且在标准中添加一些内容可能很难,因为不同的人可能想要不同的东西。

So most probably you won't be able to rely on these tests. And adding something to the standard may be hard because different people may want different things.

我想要的是检查对象是否是普通对象的某种方式。也就是说,它具有必须由所有对象支持的基本内部方法的默认行为。

What I would like is some way to check if an object is an ordinary one. That is, it has the default behaviour for the essential internal methods that must be supported by all objects.

一旦你知道一个对象是普通的,你可以依赖东西像[[GetPrototypeOf]]一样根据自己的喜好自定义测试。

Once you know that an object is ordinary, you can rely on things like [[GetPrototypeOf]] to customize the test to your tastes.

这篇关于为什么JavaScript中没有内置方法来检查对象是否是普通对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆