日期:2014-05-16  浏览次数:20321 次

js 重载和类型检查

<html>

<head>
<script type="text/javascript">
function sendMessage(msg,obj){
//js 的每一个函数存在一个上下文相关的arguments的变量
//类似以一个伪数组 包含了传给函数的所有参数,不能对其进行修改
if(arguments.length==2)
?? ?obj.handleMsg(msg);
else
??? ?alert(msg);

if(typeof msg == "string")
?? ?alert("msg type is string");
if(typeof msg == "object")
?? ?alert("object");
if(msg.constructor == String)
?? ?alert("mas.constructor is String");
}
debugger;//js 调试断点
sendMessage("hello");
sendMessage("how are you js",{handleMsg:function(msg){alert("this is cous message"+msg);}});
</script>
<body>


</body>

</head>

</html>