下面的代码:
JavaScript部分:的Javascript:以不同的方式调用对象的方法
ContatoreCostr = function(nome){
this.nome = nome;
this.cont = 0;
this.inc = function() {
alert(this + "--" + this.nome + "--" + this.cont++);
};
}
var ccc= new ContatoreCostr("zio");
window.onload = function() {
//document.getElementById("bid").onclick = ccc.inc; // DO NOT WORK
document.getElementById("bid").onclick = function(){ccc.inc()}; //WORKS
}
HTML部分:
<button onClick="ccc.inc()">Buttton1</button>
<button id="bid">Bottone2</button>
这里有两种不同的方式来调用内部的相同方法相同的对象,事实上'继续'属性继续增加任何按钮被点击。上下文改变:点击Button1时是窗口,点击Button2时是tagButton,但没关系。 我不明白为什么我被迫分配函数(){ccc.inc()}而不是ccc.inc那。在我看来,应该没有区别。 Tnx
不,如果你把括号inc()在加载页面时进行评估,那不是你想要的。 – chairam 2012-02-16 16:19:58