javascript - Difference between Constructor pattern and Prototype pattern -
so i'm trying wrap head around different ways create object.
i came accross protoype pattern creating objects.
now wrote 2 functions below can't see functional difference between both be? when use constructor pattern , when use prototype pattern?
constructor pattern
function fruit(){} fruit.color = "yellow", fruit.fruitname = "banana", fruit.nativeto = "somevalue"
prototype pattern
function fruit(){} fruit.prototype.color = "yellow", fruit.prototype.fruitname = "banana", fruit.prototype.nativeto = "somevalue"
reusability of components...
constructor
when create new constructor create new instance of , importantly change made instances affect them , not others.
prototype
when create new object using prototype reuse logic , change prototype chain affect else.
this nice explanation: javascript prototypes , instance creation
when use each pattern based on needs - ambiguous answer never-the-less situation.
think of object, function, array used throughout js , make sense live on prototype chain changes have them want propagate - side note: why should never alter these can screw behavior.
best explanation here: javascript constructors, prototypes, , new
keyword
Comments
Post a Comment