<html>
<head>
<title>JavaScript Packing Example</title>
<script 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(){
return "msg=" + this.msg;
}
}
function test(){
jscript.ui.alerts.showErrorAlert();
var v = jscript.ui.alerts.MessageDisplayer("Hello!");
alert(v);
}
</script>
</head>
<body>
<input type = "button" value = "Test Alert" onclick = "test()"/>
</body>
</html>
代码如上,求教几个问题:1.为什么第二个弹窗显示的不是Hello,而是Undefined?
2.this.msg和this.toString代表什么意思?
<head>
<title>JavaScript Packing Example</title>
<script 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(){
return "msg=" + this.msg;
}
}
function test(){
jscript.ui.alerts.showErrorAlert();
var v = jscript.ui.alerts.MessageDisplayer("Hello!");
alert(v);
}
</script>
</head>
<body>
<input type = "button" value = "Test Alert" onclick = "test()"/>
</body>
</html>
代码如上,求教几个问题:1.为什么第二个弹窗显示的不是Hello,而是Undefined?
2.this.msg和this.toString代表什么意思?
2. this.msg意思是jscript.ui.alerts.MessageDisplayer 这个类有一个成员变量 msg
this.toString意思是jscript.ui.alerts.MessageDisplayer 这个类有一个成员函数 toString所以比较矛盾,你把jscript.ui.alerts.MessageDisplayer 写成一个类,却又把它当函数调用:
jscript.ui.alerts.MessageDisplayer("Hello!");你需要google下js 怎么定义一个类,然后看var v = new jscript.ui.alerts.MessageDisplayer("Hello!");
alert(v);这样会是什么结果?另外发帖时候把代码放代码标签里
哪个调用它,哪个就是那个this
this是代词,代指调用的对象