I read through several other articles on prototypal vs pseudoclassical instantiation and still have a few questions. I have included code below for illustration.
Questions are:
Are there any significant performance issues I should be aware of?
var Person = function() {
hopes: function(){},
dreams: function(){}
};
var John = Object.create(Person); // not supported in older browsers
--
var Human = function() {
};
Human.prototype.hopes = function(){}; // could add both hopes & dreams with an object
Human.prototype.dreams = function(){};
var Alice = new Human();
Your syntax is a bit off in the first example, but yes, these will result in objects that are similar. In the first example, it should be:
var Person = {
hopes: function(){},
dreams: function(){}
};
There are some subtle differences though. Using the new keyword causes an object to be added to the prototype chain, e.g. Alice will now use Human's prototype chain. Object.create sets the object's prototype to the parameter, e.g. Person is John's prototype. These generally will be similar, except if you do Object.create(null), in which case you don't get the basic object prototype as if you did new Object();
You can run performance tests here http://jsperf.com/obj-create-vs-new/4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With