• JavaScript高级篇之Function对象


    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>

     

  • 相关阅读:
    147
    UVA12230 过河 Crossing Rivers
    重聚
    网络流24题 P2762 太空飞行计划问题
    网络流24题 P2756 飞行员配对方案问题
    网络流24题
    洛谷 P3369 【模板】普通平衡树
    LOJ #6280. 数列分块入门 4
    LOJ #6279. 数列分块入门 3
    LOJ #6278. 数列分块入门 2
  • 原文地址:https://www.cnblogs.com/zyxsblogs/p/9985924.html
Copyright © 2020-2023  润新知