【发布时间】:2016-12-03 10:09:23
【问题描述】:
对不起,这个令人困惑的标题,我不知道如何问这个。 我有一个关于类和方法的问题。我有一个按钮,按下时会调用另一个对象的函数。对于此示例,它应该将“100”作为 f 的“x”记录到控制台,但它会记录“0”:我的按钮的“x”。帮忙?
<script>
function fun(x){
this.x = x;
}
fun.prototype = {
s:function(){
console.log(this.x);
}
}
function Button(func){
this.x = 0;
this.y = 0;
this.w = 1000;
this.h = 1000;
this.func = func;
}
Button.prototype = {
check_if_click:function(x, y){
if (x >= this.x && x <= this.x + this.w && y >= this.y && y <= this.y + this.h){
this.func();
}
}
}
f = new fun(100);
b = new Button(f.s);
b.check_if_click(500, 500);
</script>
【问题讨论】:
标签: javascript function class methods arguments