javascript - difference between prototype being assigned an object literal vs function -
this question has answer here:
- defining javascript prototype 5 answers
i found article on web confusing me lot.
http://jonathancreamer.com/object-literals-and-prototypes-in-javascript/
when run following code
case2: pizza.prototype = { divvyup: function () { return this.type + " pizza has " + this.slices +" slices"; } }; versus case1: pizza.prototype.divvyup = function () { return this.type + " pizza has " + this.slices +" slices"; };
the conundrum though pizza.prototype.constructor in case1: constructor function , pizza.prototype.constrcutor in case2: object() function , case2: decouples inheritance chain. why when execute sausagepizza.divvyup() both cases same result. when case2: decoupling inheritance chain , not referencing pizza() constructor more why result case2 similar case1
the former replace entire prototype
object containing single function - divvyup
.
the latter modify existing prototype
, adding (or replacing) function divvyup
.
try
console.log(pizza.prototype);
after each , have looksie.
Comments
Post a Comment