Function函数(function方法更新[默认值,不确定参数,箭头函数])DefaultParameters-如何处理函数参数的默认值?ES5ES6函数参数个数RestParameter-如何处理不确定参数?ES5ES6SpreadOperator(rest参数的逆运算)ES5ES6ArrowFunctions(箭头函数)声明只有一个参数时可以加参数,括号可以省略。什么情况下可以省略花括号。谁定义谁指向谁ES6-ES10学习布局函数方法更新【默认值,不确定参数,箭头函数】DefaultParameters-如何处理函数参数的默认值?判断函数是否有默认值不能用||b、只有是否等于undefinedES5//x是必填值,y和z不是必填functionf(x,y,z){if(y===undefined){y=7}if(z===undefined){z=42}returnx+y+z}console.log(f(1))//50console.log(f(1,8))//51console.log(f(1,8,43))//52ES6函数参数从左到右解析,如果没有默认值,则解析为undefined//x是强制值,y和z不是必需值functionf(x,y=7,z=42){returnx+y+z}console.log(f(1))//50console.log(f(1,8))//51console.log(f(1,8,43))//52Howtomakeyonlytakethedefaultvalue//将默认值放在最后//可以看出原理类似于ES5console.log(f(1,undefined,43))另外为常量,默认值也可以是其他参数的运算表达式functionf(x,y=7,z=x+y){returnx*10+z}console.log(f(1,undefined,2))//12--1*10+2console.log(f(1))//18--1*10+(1+7)console.log(f(1,undefined))//18--1*10+(1+7)函数参数个数ES5使用参数表示函数parameters伪数组arguments.length表示参数个数functionf(x,y,z){returnarguments.length}console.log(f(1))//1console.log(f(1,undefined))//2console.log(f(1,undefined,2))//3ES6不支持arguments//length是函数没有默认值的参数个数,不是函数f执行过程中传入的参数个数(x,y=7,z=x+y){returnf.length}console.log(f(1))//1console.log(f(1,undefined))//1console.log(f(1,undefined),2))//1RestParameter——如何处理不确定参数?ES5使用argumentsfunctionsum(){letnum=0//遍历的两个方法//Array.prototype.forEach.call(arguments,function(item){//num+=item*1//})Array.from(arguments).forEach(function(item){num+=item*1})returnnum}console.log(sum(1,2,3))//6ES6使用Rest数组表示所有参数,可以是反汇编表示部分参数其余参数只能使用一次,放在最后一个位置0//直接把nums.forEa当作数组遍历ch(function(item){num+=item*1})returnnum}console.log(sum(1,2,3))//6//第一个参数×2+剩余参数functionsum(base,...nums){letnum=0nums.forEach(function(item){num+=item*1})returnbase*2+num}console.log(sum(1,2,3))//7——1*2+5SpreadOperator(restparameter的逆运算)SpreadOperator和RestParameter是形状相似但含义相反的运算符。简单来说,RestParameter将可变参数“收敛”成一个数组,而SpreadOperator则将固定数组“打散”成相应的参数。有一个数组,作为一个参数应该算是一个整体。ES5//计算三角形的周长函数sum(x=1,y=2,z=3){returnx+y+z}letdata=[4,5,6]console.log(sum(data[0],data[1],data[2]))//15控制台。log(sum.apply(this,data))//15//apply()方法:接受两个参数,一个是函数运行的范围,一个是参数数组(可以是实例数组或参数对象),将作为参数传递给函数(data->arguments)//定义:该方法调用具有给定this值的函数,参数作为数组(或类数组)提供目的)。//call()方法和apply()方法的区别在于接受参数的方式。使用call方法时,传递给函数的参数必须一一列出。console.log(sum.apply(null,[4,5,6]))//15ES6console.log(sum(...data))//15ArrowFunctions(箭头函数)()=>{}声明了左括号是参数,括号内是函数体//ES5声明函数functionhello(){}lethello=function(){}//ES6lethello=()=>{console.log('helloworld')}hello()//helloworld参数的话,可以加参数lethello=(name)=>{console.log('helloworld'+name)}hello('beijing')//当helloworldbeijing有且只有一个参数,括号可以省略lethello=name=>{console.log('helloworld'+name)}hello('beijing')//helloworldbeijing什么情况下大括号可以省略return是一个表达式(没有花括号时,表达式公式的值会自动返回,有花括号,必须写return)letsum=(z,y,z)=>x+y+zconsole.log(sum(1,2,3))//6//返回和花括号都可以省略。返回的是一个对象,必须用括号括起来letsum=(x,y,z)=>({x:x,y:y,z:z})//括号是函数表达式的意思,此时的花括号表示其他情况下这个对象的指向,老老实实写this的指向,不是谁调用给谁,谁定义指向谁。也就是说,箭头函数并没有改变this//ES5lettest={name:'test',say:function(){console.log(this.name)}}test.say()//test//ES6lettest={name:'test',say:()=>{console.log(this.name)}}test.say()//undefined//因为箭头函数中this的处理是定义好的,this的point就是测试外层指向的窗口,而这个窗口并没有有name属性,所以是未定义的学习布局
