如何在执行期间取得类的实例名称?
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> 新建网页 1 </title>
</head>
<body>
<script lang=javascript>
var aaa = new myfunc();
aaa.showtxt();
function myfunc(){
this.showtxt=function(){
document.write( "Please <span style= 'cursor:hand ' onclick=\ "aaa.showmsg( 'message! ')\ "> Click here </span> to open message! ");
};
this.showmsg=function(msg){
alert(msg);
};
}
</script>
</body>
</html>
我希望达到的效果是在语句
document.write( "Please <span style= 'cursor:hand ' onclick=\ "aaa.showmsg( 'message! ')\ "> Click here </span> to open message! ");
中的aaa.showmsg( 'message! ')的aaa这个实例名能通过某种变量或属性动态获得。
------解决方案--------------------可以设置一个局部变量,指向 对象本身.
function myfunc(){
var self =this;
this.showtxt=function(){
document.write( "Please <span id= 'test ' style= 'cursor:hand ' > Click here </span> to open message! ");
document.getElementById( 'test ').onclick=function(){self.showmsg( 'message ');};
};
this.showmsg=function(msg){
alert(msg);
};
}