2

For some reason I came across a case where Chrome's V8 seems to think that an object is not actually an Object. I can't reproduce this easily but basically I have this code segment:

if (!(obj instanceof Object)) {
    console.log(obj);
}

And yet chrome's console shows that it is in fact an Object... Does anyone know what could possibly cause this? How can I reproduce this?

WHen I use the typeof operator, it correctly identifies it as an object.

2
  • What does console.log(obj) show? Commented Jul 18, 2013 at 7:10
  • can you paste obj ? Commented Jul 18, 2013 at 7:11

6 Answers 6

4

It may be from a different window (e.g. an iframe), which has an own Object constructor

var obj = frame.contentWindow.obj;
console.log(obj instanceof Object); // false
console.log(obj instanceof frame.contentWindow.Object); // true

http://jsfiddle.net/JnfPR/1/

Also note that there is a variety of objects in JS, including "Array objects", "RegExp objects", "Function objects", ... and "Object objects". The typeof operator is not very helpful there. It can only distinguish between Function objects and other objects.

Sign up to request clarification or add additional context in comments.

1 Comment

I think that explains it - The project does involve iframes... Very unusual issue. Thanks for that explanation.
1

This can happen if you override __proto__. For example:

var myObj = { __proto__: null };

(myObject instanceof Object) === false;

This is due to how instanceof works – it basically walks up the prototype chain looking for the constructor you passed to it. More information about instanceof is available here.

Comments

1

An object need not be an instance of Object in JavaScript. For example:

var obj = Object.create(null);
console.log(obj instanceof Object); // false
console.log(typeof obj);            // object

The reason for this is because of the way instanceof is implemented. See the following answer for more details: https://stackoverflow.com/a/8096017/783743

Comments

0

Typeof gives you the type of an object (in this case "object") and instanceof tests the prototype chain.

o={};
o instanceof Object //is true
typeof o // "object" 
typeof(o.constructor.prototype)==="object" // is true

So in your case, the error has to be in the obj.

Comments

0

You're most likely dealing with a null value:

console.log(null instanceof Object);//false
console.log(typeof(null));// object

Or as Pumbaa80 commented; you got the object from an Iframe so it's constructor isn't the same Object as in the main window therefor instanceof Object is false but the variable does have an object value.

Comments

0

The instanceof operator tests whether an object has in its prototype chain the prototype property of a constructor.

You can check Mozilla's developer site.

Basically the instanceof operator evaulates to true if the object inherits from the classe's prototype, so for example:

 var a = new Animal():

Here a is instance of Animal since a inherits from Animal.prototype

1 Comment

While this answer does correctly explain instanceof behavior, it does not explain how an object could be missing Object.prototype from its prototype chain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.