Is this correct (JavaScript prototype property)? -
here created function used constructor other clones. aside properties arguments, caller, length, name, __proto__(link function.protoype) created, prototype property created. property points object assigned prototype instances created when function invoked when using new keyword.
function clonetrooper(id, rank, yearsofservice, type){ if (!(this instanceof clonetrooper)){ return new clonetrooper(id, rank, yearsofservice, type); } this.id = id; this.rank = rank; this.yearsofservice = yearsofservice; this.type = type; } so when this:
clonetrooper.prototype.hasutilitybelt = true; clonetrooper.prototype.haslightsaber = false; i adding properties on clonetrooper prototype property. 1 one.
but if this afterwards:
clonetrooper.prototype = { name: null hasdroid: null }; i have overwritten link clonetrooper's constructor replacing new object links object.prototype.
so question is, last syntax indeed overwriting prototype property?
so 1 should plan out object avoid occurrence.
yes, must careful. in cases should extend existing prototype 1 of 2 ways:
clonetrooper.prototype.foo = ... or (e.g. using jquery)
$.extend(clonetrooper.prototype, { bar: ..., baz: ... })
Comments
Post a Comment