<div id="id1">Click to know</div>
<script>
function Superheroe(power){
this.power=power;
}
//edited: copy mistake solved
Superheroe.prototype.sayYourPower=function(){
alert('This is my power :'+ this.power);
}
var Mike = new Superheroe ("Code fast");
document.getElementById('id1').addEventListener("click",Mike.sayYourPower);
</script>
我很想知道为什么当我要求sayYourPower时我得到'undefined'...这是为了在方法结束时不包括“()”?javascript测试中的简单原型
您将函数添加到'Person'原型,但是从'Superheroe'创建对象。并且即使你将'Person'改为'Superheroe'也不行,因为在你的情况下'this this'里面的事件处理器并不是指对象'超级英雄' – Grundy
我犯了一个错误对不起! –