1
var foo = Object.create(null); //complete empty
var bar = {}; //empty object
var don = function(){
    //like template, need new to create an object
};

console.log(foo); //Object (no properties)
console.log(bar); //Object (__proto__)
console.log(new don); //don{} (__proto__)

I'm new in javascipt oop, I got a question about object type.

what are different and how to use those object?

1

2 Answers 2

2

foo is an object without prototype and properties.

bar is a new object inherited from Object. It inherit all properties and method of Object.

don is an constructor of object. new don() will create an object and set property constructor to don in the new object.

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

1 Comment

bar is inherited from obj, which obj?
0

{} is equivalent to Object.create(Object.prototype), it inherit all the properties and methods from Object.prototype, including 'isPrototypeOf' and 'hasOwnProperty' method or property.
Object.create(null) creates an object that doesn't inherit any property or method, so if you use it like this: Object.create(null).hasOwnProperty('xx'), it will trigger an error: "Object doesn't support property or method 'hasOwnProperty' ".
Object.prototype.isPrototypeOf(function(){}.prototype) returns true, it means that 'new don()' also inherit all the properties and methods from Object.prototype.
The constructor of Object.create(null) & {} is Object, howerver, the constructor of 'new don()' is function(){}.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.