new operator VS object.create( )
In object oriented programming, new operator and object.create( ) method are both used to create new objects; they are both reusing codes for convenience; they both inherit from another object. But what’s the difference, and when should we use one over the other? The blog post is going to talk about these.
MDN definitions:
“The new operator creates an instance of a user-defined object type or of one of the built-in object types that has a constructor function.”
The new operator is using class inheritance: a class is like a blueprint - a description of the object to be created. It forms parent/child hierarchies.
“The Object.create( ) method creates a new object with the specified prototype object and properties.”
The object.create( ) is using prototypal inheritance: a prototype is a working object instance. Objects inherit directly from other objects. Basically a copy.
Syntax:
new constructor[([arguments])]
object.create(proto,[propertiesObject])
When using new operator, we are inheriting everything in that class; when using object.create( ) method, we tend to create different objects contain different properties and functions, then mix and match them together.
Conclusion:
new operator is most useful when you know the whole picture at the very beginning, for example when you are creating a object oriented library.
object.create( ) method is more flexible to create objects, mix and match different properties and functions.
Reference:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/new
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/create
https://medium.com/javascript-scene/master-the-javascript-interview-what-s-the-difference-between-class-prototypal-inheritance-e4cd0a7562e9#.kfnexhg1j
http://xahlee.info/js/js_creating_object.html










