0

我试图在每次鼠标点击时调用所有按钮对象的方法,但我一点也不熟悉 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来实现?

4

2 回答 2

2

好的,有几件事。

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

// 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);
    }
}
于 2012-12-08T00:26:18.187 回答
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); 
}
于 2012-12-08T00:34:08.207 回答