user2913184 user2913184 - 1 year ago 97
Javascript Question

difference between prototype being assigned an object literal vs function

I found this article on the web which is confusing me a lot.

When I run following code

Pizza.prototype = {
divvyUp: function () {
return this.type + " pizza which has " + this.slices +" slices";

Versus case1:

Pizza.prototype.divvyUp = function () {
return this.type + " pizza which has " + this.slices +" slices";

The conundrum is that though Pizza.prototype.constructor in case1: is the constructor function and Pizza.prototype.constrcutor in case2: is object() function and case2: decouples the inheritance chain. But then why when I execute
sausagePizza.divvyUp() for both the cases i get the same result. However when case2: is decoupling the inheritance chain and not referencing to Pizza() constructor any more then why do I get the result for case2 similar to case1

Answer Source

The former will replace the entire prototype with an object containing a single function - divvyUp.

The latter will modify the existing prototype, ADDING (or replacing) the function divvyUp.



after each and have a looksie.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download