day3-1函数

函数:

如果写在对象内,是一个方法

函数声明

function 函数名(形参列表){

//函数体

}

函数表达式

var 函数名 = function  (形参列表){

//函数体

}

匿名函数  function(){}

函数的执行

test(参数列表);

//this 代表函数执行所依赖的环境对象。谁调用函数,this就是谁

test.call(this,参数列表);

test.apply(this,参数数组(参数以数组形式))

 

// ()代表运行函数

function test(){

console.log(1);

return function test2(){

console.log(2);

}

}

//test() 返回值是一个函数

test()();

 

函数的内部属性

Length:函数形参的个数

函数的arguments,存放了传入函数的实参

arguments :

是类数组对象,包含着传入函数中参数,arguments对象还有一个callee的属性,用来指向拥有这个arguments对象的函数

函数中的局部变量和全局变量:

使用var操作符定义的变量将成为定义该变量的作用域中的局部变量。

       function test(){

var message = "hello";

       }

       test();

       alert(message); //错误

     2) 如果在函数中定义变量没有加var,该变量为全局变量

       function test(){

message = "hello";

      }

       test();

       alert(message); //可以访问

//参数可有默认值

function add(a,b,c,d=10){

return a+b+c+d;

}

表示d有默认值为10,若传了参数给d,则为传参值

原文地址:https://www.cnblogs.com/wskb/p/11089599.html