Javascript 调用所有对象的函数
我试图在每次鼠标点击时调用一个包含所有按钮对象的方法,但我对javascript原型的工作方式一点也不熟悉,非常感谢您的帮助。这是我到目前为止所拥有的Javascript 调用所有对象的函数,javascript,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.heigh
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);
}
var button1=新按钮(200200150150,“testFunc();”);
功能按钮(x、y、宽度、高度、功能){
这个.x=x;
这个。y=y;
这个。宽度=宽度;
高度=高度;
this.func=func;
}
button.prototype.click=函数(clickx,clicky){
eval(this.func)
console.log(“单击“+this.x+”+clicky”处的按钮);
如果(点击x>this.x&&(点击x+width)this.y&(clicky+height)
我基本上希望每个按钮对象都使用单击的xy坐标检查它是否被单击。当然,这应该可以通过javascript实现?好的,有几件事
按钮
eval
//构造函数!资本化!
功能按钮(x、y、宽度、高度、功能){
这个.x=x;
这个。y=y;
这个。宽度=宽度;
高度=高度;
this.func=func;
}
//每个构造的按钮都有一个click()方法。
Button.prototype.click=函数(clickx,clicky){
console.log(“单击“+this.x+”+clicky”处的按钮);
如果(点击x>this.x&&(点击x+width)this.y&(clicky+height)
我假设您的代码是针对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);
}
var button1=新按钮(200200150150,“testFunc();”);
功能按钮(x、y、宽度、高度、功能){
这个.x=x;
这个。y=y;
这个。宽度=宽度;
高度=高度;
this.func=func;
}
button.prototype.click=函数(clickx,clicky){
eval(this.func)
console.log(“单击“+this.x+”+clicky”处的按钮);
如果(点击x>this.x&&(点击x+width)this.y&(clicky+height)
eval(this.func)
wat<代码>评估是邪恶的。非常感谢您的帮助!
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);
}