javascript prototype -
i trying understand js prototype property: sample code
function container(param) { this.member = param; } var newc = new container('abc'); container.prototype.stamp = function (string) { return this.member + string; } document.write(newc.stamp('def')); function box() { this.color = "red"; this.member = "why"; } container.prototype = new box(); box.prototype.test = "whatever"; var b = new box(); document.write(newc.test);
here last line undefined - though container's prototype box , box's prototype has property test, why newc refers test in box doesnt work? can 1 please explain how 'prototype' works in above context.
thanks...
you setting container
prototype box()
after newc
instance created.
reorder statements follows:
function container(param) { this.member = param; } function box() { this.color = "red"; this.member = "why"; } container.prototype = new box(); box.prototype.test = "whatever"; container.prototype.stamp = function (string) { return this.member + string; } //here containers prototype setup complete. var newc = new container('abc'); document.write(newc.stamp('def')); document.write(newc.test);
Comments
Post a Comment