JS的函数对象

创建方式:1、function f() { }

     2、var obj = new Function(“参数1”,“参数n”,“function body”)

先加载后执行

f.length 参数的数量

函数的调用:

arguments可获取所有参数

匿名函数:1、var func=function(arg){ }  func()

     2、(function(arg)){alert(arg)})("Yuan")

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>


<script>


////    函数的创建方式一
//
//
//    function f(x,y) {
//        alert(123);
//        return x+y;           //null
//    }
//    console.log(f(23,5678));
//
//
//    foo("瞎驴");
//    function foo(name,age) {
//        console.log("hello"+name)
//    }
//
//
//
////    函数的创建方式二
//
//    var obj=new Function("name","console.log(\"hello\"+name)")
//    obj("武大");
//
//    console.log(foo.length)
//
//// 函数的调用
//
//
//    function add(x,y,z) {
//        return x+y+z
//    }
//
//    console.log(add(1,2,3,4,5,6));
//    console.log(add(1,2));
//    console.log(add("hello","world"));


//  面试题
//    function f(x,y) {
//        return x+y
//    }
//
//    var f=1;
//    var b=2;
//    f(f,b)

 // arguments

//      function f() {
//          console.log(arguments);
//
//          return a+b
//      }
//      f(1,2,45,6,7,78);

//    function ADD() {
//          var sum=0;
//        for (var i=0;i<arguments.length;i++){
//            sum+=arguments[i]
//        }
//        return sum
//    }
//
//    console.log(ADD(1,2))

// 匿名函数


//        var func = function(arg){
//            alert(arg)
//            };
//
//        func("hello");

(function(arg){
           alert(arg)
           })("YUAN")



</script>
</body>
</html>
View Code

猜你喜欢

转载自www.cnblogs.com/jintian/p/11106238.html