开发者

Initiation of an object inside another

I have a piece of js software that is structured like 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 = funct开发者_如何学Goion (e) {
        if (this.started) {

        }
    }
}

The above code block is duplicated for multiple shapes so there is also a obj.square obj.circle etc...

I also have a shape object that is as 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 would like to initiate the shape object on a mousedown for each obj.* and populate the shape object with the propper info.

Now for the issue.

The radius is calcuated on every mousemove as well as height and width but when I add shapes = new Shape(circle, black, 10, 10, null, e._x, e._y) to the mousemove so it looks like...

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

The shape object does not create.

If I create the shape object inside the wrapper function instead of the mousemove then the object initiates but I cannot use radius or height/width. How can I create an object inside another object inside a wrapper function so I can use calculated terms inside the created object? Is there an alternate route to take besides what I am doing?


Aside from wonkiness in the obj = new object[this.id](); line, I think you're just missing a this keyword:

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

Edit just noticed more wonkiness in your code (yes, that's a technical term :). I think you want to change these lines in the constructor:

this.points = ["x","y"]; // creates an array, which is indexed by numbers
this.points["x"] = [x];  // tacks on some ad-hoc properties to the array, which
this.points["y"] = [y];  // doesn't really make sense

to this:

this.points = {x: x,   // I think this is what you actually mean to do.
               y: y};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜