调用所有对象的函数

时间:2012-12-08 00:11:51

标签: javascript

我试图在每次点击鼠标时调用所有按钮对象的方法,但我对javascript原型的工作原理并不熟悉,非常感谢一些帮助。这是我到目前为止所拥有的。

var button1 = new button(200, 200, 150, 150, "testFunc();"); 
function button(x,y,width,height, func) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.func = func;

}

button.prototype.click = function(clickx, clicky) {
    eval(this.func)
        console.log("Clicked button at" + this.x + " " + clicky);
        if (clickx > this.x && (clickx + width) < this.x) {
            if (clicky > this.y && (clicky + height) < this.y) {

                this.func(); //Call the button's function
            }
        }   
}

function onClick(x, y) {
    button.prototype.click.call(x, y); 
}

我基本上希望每个按钮对象检查是否使用点击的xy坐标点击了它。当然这应该可以使用javascript吗?

2 个答案:

答案 0 :(得分:2)

好的,有些事情。

  1. 构造函数应始终以大写字母开头。 Button不是button
  2. 函数是对象,您可以使用eval直接传递一个永远
  3. 如果要操作按钮列表,则需要一个按钮列表。原型中的函数与所有实例共享,但您无法从原型中获取所有实例。您需要自己维护该列表。数组适用于列表。

  4. // Constructor! capitalized!
    function Button(x, y, width, height, func) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.func = func;
    }
    
    // Each constructed Button will have a click() method.
    Button.prototype.click = function(clickx, clicky) {
        console.log("Clicked button at" + this.x + " " + clicky);
        if (clickx > this.x && (clickx + width) < this.x) {
            if (clicky > this.y && (clicky + height) < this.y) {
                this.func(); //Call the button's function
            }
        }   
    }
    
    // Passed as the function to execute in this not very exciting example.
    function testFunc() {
        console.log('called testFunc()!')
    }
    
    // Make a bunch of buttons, save them in an array.
    // Note how we actually pass a function object as the last argument.
    // Note how the last argument IS NOT a string.
    var buttons = [
      new Button(100, 100, 150, 150, testFunc),
      new Button(250, 250, 150, 150, testFunc),
      new Button(400, 400, 150, 150, testFunc)
    ];
    
    // called by something else that passes x and y
    function onClick(x, y) {
        // tell each button we had a click, and pass in x and y to see
        // if it should handle it.
        for (var i = 0; i < buttons.length; i++) {
            var button = buttons[i];
            button.click(x, y);
        }
    }
    

答案 1 :(得分:0)

我认为你正在编写针对dom的编码。

要点击你的按钮需要成为一个dom元素,你的DOM元素在哪里定义? DOM是您需要编写代码的API。你不能只是魔术让你的随机按钮对象听取点击事件,如果它不遵循文档对象模型...这与javascript原型无关。

var domElement = document.createElement("DIV")

domElement.onclick = function(event){
 // do some stuffs
  alert("clicked at"+event.pageX+" "+event.pageY);
}

我引用您的代码:

var button1 = new button(200, 200, 150, 150, "testFunc();"); 
function button(x,y,width,height, func) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.func = func;

}

button.prototype.click = function(clickx, clicky) {
    eval(this.func)
        console.log("Clicked button at" + this.x + " " + clicky);
        if (clickx > this.x && (clickx + width) < this.x) {
            if (clicky > this.y && (clicky + height) < this.y) {

                this.func(); //Call the button's function
            }
        }   
}

function onClick(x, y) {
    button.prototype.click.call(x, y); 
}