Default Parameters —— 如何處理函數參數默認值?數組
Rest Parameter —— 怎麼處理不肯定參數?app
Spread Operator(rest參數的逆運算)函數
Arrow Functions(箭頭函數)學習
參數狀況this
函數方法更新【默認值、不肯定參數、箭頭函數】spa
判斷函數有沒有默認值不能使用a || b,只能判斷其是否等於undefinedprototype
//x是必傳值,y和z是沒必要傳值 function f (x, y, z) { if ( y === undefined) { y = 7 } if ( z === undefined) { z = 42 } return x + y + z } console.log(f(1)) // 50 console.log(f(1, 8)) // 51 console.log(f(1, 8, 43)) // 52
函數參數是從左到右解析,若是沒有默認值會被解析成undefinedrest
//x是必傳值,y和z是沒必要傳值 function f (x, y = 7, z = 42) { return x + y + z } console.log(f(1)) // 50 console.log(f(1, 8)) // 51 console.log(f(1, 8, 43)) // 52
如何讓y只取默認值code
//有默認值的放最後 //能夠看出來原理和ES5是相似的 console.log(f(1, undefined, 43))
默認值除了常量,還能夠是其餘參數的運算表達式對象
function f (x, y = 7, z = x + y) { return x * 10 + z } console.log(f(1, undefined, 2)) // 12--1 * 10 + 2 console.log(f(1)) // 18--1 * 10 + ( 1 + 7 ) console.log(f(1, undefined)) // 18--1 * 10 + ( 1 + 7 )
ES5使用arguments表示函數參數的僞數組,arguments.length表示參數的個數
function f (x, y, z) { return arguments.length } console.log(f(1)) // 1 console.log(f(1, undefined)) // 2 console.log(f(1, undefined, 2)) // 3
ES6中不支持arguments
// length是函數沒有默認值的參數的個數,並非執行的時候傳入參數的個數 function f (x, y = 7, z = x + y) { return f.length } console.log(f(1)) // 1 console.log(f(1, undefined)) // 1 console.log(f(1, undefined, 2)) // 1
使用arguments
function sum () { let num = 0 //兩種方法進行遍歷 // Array.prototype.forEach.call(arguments, function (item) { // num += item * 1 // }) Array.from(arguments).forEach( function (item) { num += item * 1 }) return num } console.log(sum(1, 2, 3)) // 6
使用Rest
function sum (...nums) { // ... —— Rest parameter 全部的參數都在三點後面的nums變量中 let num = 0 // 直接看成數組遍歷 nums.forEach( function (item) { num += item * 1 }) return num } console.log(sum(1, 2, 3)) // 6 //將第一個參數 × 2 + 剩餘參數 function sum (base, ...nums) { let num = 0 nums.forEach( function (item) { num += item * 1 }) return base * 2 + num } console.log(sum(1, 2, 3)) // 7 —— 1 * 2 + 5
Spread Operator和Rest Parameter是形似但相反意義的操做符,簡單來講Rest Parameter是把不定參數「收斂」成數組,而Spread Operator是把固定數組「打散」到對應的參數中
有一個數組,要總體當成參數
//計算三角形周長 function sum (x = 1, y = 2, z = 3) { return x + y + z } let data = [4, 5, 6] console.log(sum(data[0], data[1], data[2])) // 15 console.log(sum.apply(this, data)) // 15 // apply()方法:接受兩個參數,一個是在其中運行函數的做用域,另外一個是參數數組(能夠是Array的實例,也能夠是arguments對象),它將做爲參數傳給Function(data–>arguments) // 定義:方法調用一個具備給定this值的函數,以及做爲一個數組(或相似數組對象)提供的參數。 // call()方法與apply()方法的區別在於接受參數的方式不一樣,使用call方法是傳遞給函數的參數必須逐個例舉出來 console.log(sum.apply(null, [4, 5, 6])) // 15
console.log(sum(...data)) // 15
() => {}
左邊括號裏面是參數,後邊括號裏面是函數體
// ES5聲明函數 function hello () {} let hello = function () {} //ES6 let hello = () => { console.log('hello world') } hello() //hello world
let hello = (name) => { console.log('hello world ' + name) } hello('beijing') //hello world beijing
let hello = name => { console.log('hello world ' + name) } hello('beijing') //hello world beijing
let sum = (z, y, z) => x + y + z console.log(sum(1, 2, 3)) // 6 //return 和花括號均可以省略
let sum = (x, y, z) => ({ x: x, y: y, z: z }) // 小括號是函數表達式的意思,這個時候的花括號表示對象
換句簡單的話說,箭頭函數不改變this的指向
// ES5 let test = { name: 'test', say: function () { console.log(this.name) } } test.say() // test // ES6 let test = { name: 'test', say: () => { console.log(this.name) } } test.say() // undefined // 由於箭頭函數中對this的處理是定義時,this的指向也就是test外層所指向的window,而window沒有name屬性,因此是undefined