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?

Benjamin W
  • 2,658
  • 7
  • 26
  • 48
  • Possible duplicate of [Prototypical OO in JavaScript](http://stackoverflow.com/questions/6526241/prototypical-oo-in-javascript) – raam86 Dec 26 '16 at 11:43

2 Answers2

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.

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(){}.

Yichong
  • 707
  • 4
  • 10