JS里一个伪包的例子,求解释...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript" type="text/javascript">
jscript = function(){}
jscript.ui = function(){}
jscript.ui.alerts = new function(){}
jscript.ui.alerts.showErrorAlert = function(){
alert("An error occurred");
}
jscript.ui.alerts.MessageDisplayer = function(inMsg){
this.msg = inMsg;
this.toString = function(){ //这里的为什么要用toString啊?
return "msg= " + this.msg;
}
}
function test(){
jscript.ui.alerts.showErrorAlert();
var v = new jscript.ui.alerts.MessageDisplayer("hello");
alert(v);
}
</script>
</head>
<body>
<input type="button" value="Test" onclick="test();">
</body>
</html>
内个toString起什么作用啊?
------解决方案--------------------这里toString是不是调用函数,是定义函数,
JScript code
jscript.ui.alerts.MessageDisplayer = function(inMsg){
this.msg = inMsg;
this.toString = function(){ //定义一个toString函数,或者说重写了这个对象的toString函数
return "msg= " + this.msg;
}
}
------解决方案--------------------
toString 一个函数名称而已,随便去一个名称都可以,只有符合js命名规范