我有一个具有两个属性的基本 javascript 类。我想在点击事件中记录一个的值。这是我得到的:
function Clicker(/*string*/var1, /*id*/var2) {
this.name = var1;
this.clickerid = var2;
this.clickevent = function() {
console.log("1: " + this.name);
console.log("2: " + this);
console.log("3: " + window.testClicker.name);
};
var element = document.getElementById(this.clickerid);
element.addEventListener("click", this.clickevent, false);
}
window.onload = function() {
window.testClicker = new Clicker("lorem ipsum", "checkbox1");
};
<input id="checkbox1" type="checkbox" value="1" checked>
当我运行测试时,我在日志中看到以下内容:
1: undefined
2: <input id="checkbox1" type="checkbox" value="1" checked>
3: lorem ipsum
我期待看到第一行和第三行匹配。建议?