js 第二节 动态函数 arguments json
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title></title> </head> <body> <script type="text/javascript"> //js 里面的函数 没有重载一说 但可以通过 arguments实现重载 function argument() { for (var i = 0; i < arguments.length; i++) { console.log(arguments[i]);//a, b } switch (arguments.length) { case 0: fun1(); break; case 1: fun2('a'); break; case 2: fun3(1, 2); break; default: fun1(); break; } } function fun1() { } function fun2(num) { alert(num); } function fun3(n1, n2) { alert(n1 + n2); } //动态函数 function abc() { var obj = document.getElementById("fun").value; var fun1 = new Function(obj); //Function 大写 fun1(); }
</script>
<textarea style="width:200px;height:200px;" id="fun">
var num =12 ;
alert(typeof num);
var num ="12" ;
alert(typeof num);
</textarea>
<input type="button" value="单击" onclick="abc()" />
<input type="button" value="argu" onclick="argument('a','b')" />
</body>
</html>
//json
var obj = {
a: "zhangsan",
b: "alert(1)",
"c": function () { alert('hello js') } //有点像匿名函数
};
alert(typeof obj.c());// undefined
alert(obj.c());// hello js