JS的函数

    xiaoxiao2022-06-26  172

    (一)函数的定义 函数使用 function 声明,后跟参数以及函数体; 语法: function name(形参){ statements } name(实参); 1.()中的内容为函数的参数 2.() 中的内容不是必须的,不是语法 函数的调用规则 name(); 函数的声明提前, 在代码执行之前提前声明

    //声明函数 name(); function name(){ console.log(1); } function function name() { console.log(1); }

    (二)函数的返回值 return:任何函数通过return语句,后面跟着返回的值来实现返回值 函数有返回值: return 返回后直接跳出, 后续代码不执行

    console.log(showname()); function showname(){ //代码 return 1; }

    (三)函数的参数:arguments 分类:形参 调用函数传递的值 实参三种;

    student(1,2,3); function student(name,sex,age){ console.log(name,sex,age); }

    无参函数: arguments 函数特有的属性,里面是函数的相关参数。

    stu(1,2,3,4,5,6); function stu(){ /*arguments.length*/// 参数列表的长度 console.log(arguments[0],arguments[1],arguments[2],arguments[3]); }

    (四)自执行函数:函数没有名称

    (function (){ console.log(123); })(); (function (a,b){ console.log(a,b); })(1,2);

    (五)匿名函数:函数没有名称

    console.log(fun);//变量也可以提前声明 //fun() 报错 fun is not function //下面这种声明方式的函数 不能被提前声明 var fun=function (){ console.log(1); } fun();

    最新回复(0)