JavaScript高级篇之Function对象
一: Function对象引入:
Function对象是js的方法对象,可以用Function实例化出任何js方法对象。
例如:
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>JS</title> 8 </head> 9 <script type="text/javascript"> 10 function say(name,age){ 11 alert(name+"今年"+age+"岁了!"); 12 } 13 say("张三","18"); 14 </script> 15 <body> 16 </body> 17 </html>
二 用Function创建方法对象:
1 <script type="text/javascript"> 2 var sayFun=new Function("name","age","alert(name+'今年'+age+'岁了!')"); 3 sayFun("李四","四"); 4 </script>
三 Function 对象属性:
1 <script type="text/javascript"> 2 var sayFun=new Function("name","age","alert(name+'今年'+age+'岁了!')"); 3 alert("sayFun方法的参数个数为"+sayFun.length); 4 </script>
四 Function对象方法:
1 <script type="text/javascript"> 2 var sayFun=new Function("name","age","alert(name+'今年'+age+'岁了!')"); 3 alert(sayFun.toString());//Function仍然具有object对象的方法! 4 </script>
效果一样!!
1 <script type="text/javascript"> 2 var sayFun=new Function("name","age","alert(name+'今年'+age+'岁了!')"); 3 alert(sayFun.valueOf());//Function仍然具有object对象的方法! 4 </script>