错误点在this.alertTest = function(){
alert("Hard code Test");
alert("Id:"+this.sId);
} 因为this.sId是属于CTest类的
但是你在方法里引用了this.sId,这个时候的this是属于alertTest方法的this
也就是说此时的this针对的是this.alertTest = function(){
但你在这个方法里未对它赋值
所以是undefined
alert("Hard code Test");
alert("Id:"+this.sId);
} 因为this.sId是属于CTest类的
但是你在方法里引用了this.sId,这个时候的this是属于alertTest方法的this
也就是说此时的this针对的是this.alertTest = function(){
但你在这个方法里未对它赋值
所以是undefined
<body>
<div id="testScreen"> 测试测试 </div>
<script language="javascript" type="text/javascript">
function CTest(sId){
this.sId = sId;
this.alertTest = function(){
alert("Hard code Test");
alert("Id:" + sId);
}
this.divObj = document.getElementById(this.sId);
this.divObj.onclick = this.alertTest;
}
var objTest = new CTest("testScreen");
</script>
</body>
function CTest(sId){
this.sId = sId;
this.alertTest = function(){
this.sId = sid;
alert("Hard code Test");
alert("Id:"+this.sId);
}
this.divObj = document.getElementById(this.sId);
this.divObj.onclick = this.alertTest;
}
var objTest = new CTest("testScreen");
或者直接
function CTest(sId){
this.sId = sId;
this.alertTest = function(){
alert("Hard code Test");
alert("Id:"+sId);
}
this.divObj = document.getElementById(this.sId);
this.divObj.onclick = this.alertTest;
}
var objTest = new CTest("testScreen");
jf jf
jf