2

I ran the following in node.js v0.10.22. I understand this initial object creation snippet:

> var o1 = {};
> var o2 = Object.create(null)
> var o3 = Object.create(Object.prototype);
> var o4 = Object.create({})

> o1
{}
> o2
{}
> o3
{}
> o4
{}

> o1.prototype === void 0
true
> o2.prototype === void 0
true
> o3.prototype === void 0
true
> o4.prototype === void 0
true

However the following confuses me:

> o1 instanceof Object
true
> o2 instanceof Object
false
> o3 instanceof Object
true
> o4 instanceof Object
true

What is the explanation behind this behaviour ?

3
  • Keep in mind that object instances do not have a property prototype to access their prototype - only their constructor function would have such a property. In Chrome, for example, you can access the prototype of an object instance via o1.__proto__ Commented Feb 21, 2014 at 7:18
  • I get true for o4 instanceof Object when I run your code (node v0.10.13 as well as in Chrome browser console). Commented Feb 21, 2014 at 7:32
  • @TedHopp You are correct. I reran it & will update my question. Commented Feb 21, 2014 at 7:43

1 Answer 1

2

When you write something like that:

var ob = {}; 
// its equivalent to Object.create(Object.prototype)

When you write:

Object.create(null) 
// doesn't inherit from anywhere and thus has no properties at all.

In other words. a javascript object inherits from Object by default, unless you explicitly create it specifying null as its prototype, as in Object.create(null)

Also you must read this question and answer, i think its great

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

2 Comments

great explanation. Another great resource IMO is objectplayground.com
@HelloWorld Thanks for you, objectplayground nice resource to learn))

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.