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?
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 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.
I had the same issue, so I decide to go this way:
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.
What you want is:
This checks that the object is a plain object created with either
new Object()
or{...}
and not some subclass ofObject
.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: