Javascript prototype inheritance and Object Constructor -


i have following piece of code learning js.

function circlearea(x) {    this.x = x; }  circlearea.prototype =  {      area: function () {        return 22 / 7 * this.x * this.x;    } };  var calarea = new circlearea(7); if( calarea.constructor === circlearea.prototype.constructor) {    alert(calarea.area()); } 

i decoupled inheritance chain assigning object literal circlearea.prototype , defined calarea object using circlearea constructor. both calarea.constructor , circlearea.prototype.constructor object constructors rather circlearea constructor when called calarea.area() inside alert function this.x obtains 7 value whereas value 7 passed argument circlearea constructor not object constructor calarea.constructor , circlearea.prototype constructors refer now.

i'm not sure mean "decoupled inheritance chain" me, experiencing expected behavior.

you call area() on instance of circlearea.

area() uses this.x.

this in context instance of circlearea has x=7.

so calculation 22 / 7 * 7 * 7

if expecting different, explain expect , why expect it?


i guessing confusion stems this set object instance function called on, calling calarea.area() means this set calarea this.x same calarea.x


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -