javascript - Initiation of an object inside another -


i have piece of js software structured so

obj = new object[id]();  function wrapperfunction (e) {     var pos = findpos(this);     e._x = e.pagex - pos.x;     e._y = e.pagey - pos.y;      var func = obj[e.type];     if (func) {         func(e);                 } } 

__

obj.line = function () {         this.started = false;     this.mousedown = function (e) {      }      this.mousemove = function (e) {         if (this.started) {          }     }      this.mouseup = function (e) {         if (this.started) {          }     } } 

the above code block duplicated multiple shapes there obj.square obj.circle etc...

i have shape object follows.

function shape (type, color, height, width, radius, x, y) {     this.type = type;     this.color = color;     this.h = height;     this.w = width;      this.r = radius;     this.points = ["x","y"];     this.points["x"] = [x];     this.points["y"] = [y]; }; 

i initiate shape object on mousedown each obj.* , populate shape object propper info.

now issue.

the radius calcuated on every mousemove height , width when add shapes = new shape(circle, black, 10, 10, null, e._x, e._y) mousemove looks like...

this.mousemove = function (e) {     if (this.started) {         shapes = new shape(circle, black, 10, 10, null, e._x, e._y);     } } 

the shape object not create.

if create shape object inside wrapper function instead of mousemove object initiates cannot use radius or height/width. how can create object inside object inside wrapper function can use calculated terms inside created object? there alternate route take besides doing?

aside wonkiness in obj = new object[this.id](); line, think you're missing this keyword:

this.mousemove = function (e) {     if (this.started) {         this.shapes = new shape(circle, black, 10, 10, null, e._x, e._y);     } } 

edit noticed more wonkiness in code (yes, that's technical term :). think want change these lines in constructor:

this.points = ["x","y"]; // creates array, indexed numbers this.points["x"] = [x];  // tacks on ad-hoc properties array, this.points["y"] = [y];  // doesn't make sense 

to this:

this.points = {x: x,   // think mean do.                y: y}; 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -