Javascript has prototype based inheritance. Which means, inheritance occurs from objects directly, unlike classical inheritance which inherits from a class.
When you do this:
Foo.protoype = Bar.prototype
- you are inheriting all vars and methods from Bar
object.. but any change to Foo
's prototype will result in Bar
's prototype to be changed(since due to above statement, Foo.prototype
references Bar.prototype
).
Object.create()
is used to add classical inheritance like property to Foo
. So instead of inheriting from the object directly, you are inheriting from Object
with a specified prototype. And any change to Foo
after the statement Foo.prototype = Object.create(Bar.prototype)
will not change Bar
.
see here for more info!