Javascript 中如何判断一个对象是否是对象字面量?
有没有办法确定 Javascript 中是否使用 object-literal< 创建对象/a> 表示法还是使用构造函数方法?
在我看来,您只是访问它的父对象,但是如果您传入的对象没有对其父对象的引用,我认为您无法分辨这一点,可以吗?
Is there any way to determine in Javascript if an object was created using object-literal notation or using a constructor method?
It seems to me that you just access it's parent object, but if the object you are passing in doesn't have a reference to it's parent, I don't think you can tell this, can you?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您想要的是:
这会检查该对象是否是使用
new Object()
或{...}
创建的普通对象,而不是Object 的某个子类
。What you want is:
This checks that the object is a plain object created with either
new Object()
or{...}
and not some subclass ofObject
.我刚刚在一次甜蜜的 hackfest 中遇到了这个问题和线程,其中涉及一个 grail 任务,用于评估对象是使用 {} 还是 new Object() 创建的(我仍然没有弄清楚。)
无论如何,我很惊讶地发现了相似之处此处发布的 isObjectLiteral() 函数和我自己为 Pollen.JS 项目编写的 isObjLiteral() 函数之间。 我相信这个解决方案是在我提交 Pollen.JS 之前发布的,所以 - 向您致敬! 我的优点是长度......不到一半(当包括你的设置例程时),但两者都会产生相同的结果。
看一下:
另外,一些测试内容:
或者在 jsbin.com 上...
http://jsbin.com/iwuwa
当你到达那里时一定要打开 firebug - 调试文档是为了 IE 爱好者。
I just came across this question and thread during a sweet hackfest that involved a grail quest for evaluating whether an object was created with {} or new Object() (i still havent figured that out.)
Anyway, I was suprised to find the similarity between the isObjectLiteral() function posted here and my own isObjLiteral() function that I wrote for the Pollen.JS project. I believe this solution was posted prior to my Pollen.JS commit, so - hats off to you! The upside to mine is the length... less then half (when included your set up routine), but both produce the same results.
Take a look:
Additionally, some test stuff:
Or on jsbin.com...
http://jsbin.com/iwuwa
Be sure to open firebug when you get there - debugging to the document is for IE lovers.
编辑:我将“对象文字”解释为使用对象文字或
Object
构造函数创建的任何内容。 这很可能就是约翰·雷西格的意思。我有一个函数,即使
.constructor
被污染或者对象是在另一个框架中创建的,它也可以工作。 请注意,Object.prototype.toString.call(obj) === "[object Object]"
(正如某些人可能认为的那样)不会解决此问题。以下是测试用例的 HTML:
Edit: I'm interpreting "object literal" as anything created using an object literal or the
Object
constructor. This is what John Resig most likely meant.I have a function that will work even if
.constructor
has been tainted or if the object was created in another frame. Note thatObject.prototype.toString.call(obj) === "[object Object]"
(as some may believe) will not solve this problem.Here is the HTML for the testcase:
听起来您正在寻找这个:
对象的构造函数属性是指向用于构造它的函数的指针。 在上面的示例中,
b.constructor == Foo
。 如果对象是使用大括号(数组文字表示法)或使用new Object()
创建的,则其构造函数属性将为== Object
。更新:crescentfresh指出
$(document).constructor == Object
而不是等于jQuery构造函数,所以我做了更多的挖掘。 似乎通过使用对象文字作为对象的原型,您使构造函数属性几乎毫无价值:但是:
在另一个答案中有一个很好的解释 这里,以及更复杂的解释这里。
我认为其他答案是正确的,并且没有真正的方法来检测这一点。
It sounds like you are looking for this:
The constructor property on an object is a pointer to the function that is used to construct it. In the example above
b.constructor == Foo
. If the object was created using curly brackets (the array literal notation) or usingnew Object()
then its constructor property will== Object
.Update: crescentfresh pointed out that
$(document).constructor == Object
rather than being equal to the jQuery constructor, so I did a little more digging. It seems that by using an object literal as the prototype of an object you render the constructor property almost worthless:but:
There is a very good explanation of this in another answer here, and a more involved explanation here.
I think the other answers are correct and there is not really a way to detect this.
对象字面量是用于定义对象的符号 - 在 javascript 中始终采用大括号括起来的名称-值对的形式。 一旦执行完毕,就无法判断该对象是否是通过此表示法创建的(实际上,我认为这可能过于简化,但基本上是正确的)。 你只是有一个对象。 这是 js 的伟大之处之一,因为有很多捷径可以完成可能要写很长时间的事情。 简而言之,文字符号取代了必须编写的内容:
An object literal is the notation you use to define an object - which in javascript is always in the form of a name-value pair surrounded by the curly brackets. Once this has been executed there is no way to tell if the object was created by this notation or not (actually, I think that might be an over-simplification, but basically correct). You just have an object. This is one of the great things about js in that there are a lot of short cuts to do things that might be a lot longer to write. In short, the literal notation replaces having to write:
我遇到了同样的问题,所以我决定这样做:
I had the same issue, so I decide to go this way:
没有办法区分从对象字面量构建的对象和从其他方式构建的对象之间的区别。
这有点像问你是否可以确定一个数值变量是否是通过赋值“2”或“3-1”来构造的;
如果您需要这样做,则必须将一些特定的签名放入对象文本中以便稍后检测。
There is no way to tell the difference between an object built from an object literal, and one built from other means.
It's a bit like asking if you can determine whether a numeric variable was constructed by assigning the value '2' or '3-1';
If you need to do this, you'd have to put some specific signature into your object literal to detect later.
现在有一个更优雅的解决方案可以准确回答您的问题:
此致
Nowaday there is a more elegant solution that respond exactly to your question:
Best regards
下面所有 return false 都
比杰西的答案有所改进
below all return false
an improvement over jesse's answer
这对我有用
所有这些都会检查,没有别的:
{...}
new Object({})
Object.create(null)< /code>
Object.create({sample: 12})
This is what works for me
All of this will check, nothing else:
{...}
new Object({})
Object.create(null)
Object.create({sample: 12})
11 年前的问题是我的简洁解决方案,欢迎边缘案例建议;
步骤-> 查找对象然后比较以检查属性 -> 对象字面量没有长度、原型和边缘情况的 stringyfy 属性。
尝试测试 JSON 和
Object.create(Object.create({cool: "joes"})).
显示内部工作的另一种变体
11 year old question here is my tidy solution, open to edge case suggestions;
steps -> look for objects only then compare to check properties -> object literals do not have length, prototype and for edge case stringyfy properties.
tried in test for JSON and
Object.create(Object.create({cool: "joes"})).
Another variant showing inner working