I'm pretty sure you can't do this in ES3 with only standard support, and here's why:
Look at x
and y
creation.
var fn = function(){};
fn.prototype = {};
var x = new fn,
y = new Object;
When x
is instantiated, its internal [[Prototype]] is being set to an object referenced by fn.prototype
(just an Object
object that you assigned to fn.prototype
). Object's constructor - fn
- is then being called in a context of that newly created object, but since it doesn't mutate ab object in any way, we can consider that step irrelevant.
When y
is instantiated, its internal [[Prototype]] is being set to Object.prototype
, which is an Object
object too. Its constructor (Object
) is then being called in a context of this newly created object as well, but nothing happens there either.
So now you end up with 2 Object
objects which only differ in that their internal [[Prototype]]'s reference different objects - x
's one references whatever you assigned to fn.prototype
and y
's references Object.prototype
. Their internal [[Class]] properties are identical too (i.e. equal to "Object")
You can't get direct access to [[Prototype]] in ES3. You can infer something about it by using instanceof
, but since fn
is nulled in your example intanceof
-based inference is out of the picture.
Now, you can augment Object
constructor in such way that it would somehow mutate instantiated object. You can then inspect an object and detect this augmentation. For example:
Object = function() {
return ({ __: void 0 });
}
and then:
function isNewObject(object) {
return '__' in object;
}
but this will, of course, only work when object is being created with new Object
and not via, say, an object literal - { }
. It's also rather obtrusive :)
Perhaps there are other ways, but I can't see them right now.
HTH