石川es6课程---4、箭头函数
石川es6課程---4、箭頭函數
一、總結
一句話總結:
相當于函數的簡寫,類似python lambda 函數,先了解即可
let show1 = function () {console.log('abc') }let show2 = () => {console.log('abc') }show1() // 調用函數 show2()?
?
?
?
二、箭頭函數
- 箭頭函數,就是函數的簡寫
- 如果只有一個參數,()可以省
- 如果只有一個return,{}可以省
?
普通函數
function(){//函數體 }箭頭函數
() => {//函數體 }?
?
## 4.函數-箭頭函數- 箭頭函數,就是函數的簡寫
- 如果只有一個參數,`()` 可以省
- 如果只有一個`return`,`{}`可以省
```js
// 普通函數
function name() {
}
// 箭頭函數,去掉 function, 加上 =>
() => {
}
```
```js
let show1 = function () {
console.log('abc')
}
let show2 = () => {
console.log('abc')
}
show1() // 調用函數
show2()
let show4 = function (a) {
return a*2
}
let show5 = a => a * 2 //簡潔,類似python lambda 函數
console.log(show4(10))
console.log(show5(10))
```
?
?
三、ES6箭頭函數總結
轉自或參考:ES6箭頭函數總結
https://www.cnblogs.com/mengff/p/9656486.html
1. 箭頭函數基本形式
let func = (num) => num; let func = () => num; let sum = (num1,num2) => num1 + num2; [1,2,3].map(x => x * x);2. 箭頭函數基本特點
(1). 箭頭函數this為父作用域的this,不是調用時的this
箭頭函數的this永遠指向其父作用域,任何方法都改變不了,包括call,apply,bind。
普通函數的this指向調用它的那個對象。
上例中,init是function,以person.init調用,其內部this就是person本身,而onclick回調是箭頭函數,
其內部的this,就是父作用域的this,就是person,能得到name。
上例中,init為箭頭函數,其內部的this為全局window,onclick的this也就是init函數的this,也是window,
得到的this.name就為undefined。
(2). 箭頭函數不能作為構造函數,不能使用new
//構造函數如下: function Person(p){this.name = p.name; } //如果用箭頭函數作為構造函數,則如下 var Person = (p) => {this.name = p.name; }由于this必須是對象實例,而箭頭函數是沒有實例的,此處的this指向別處,不能產生person實例,自相矛盾。
(3). 箭頭函數沒有arguments,caller,callee
箭頭函數本身沒有arguments,如果箭頭函數在一個function內部,它會將外部函數的arguments拿過來使用。
箭頭函數中要想接收不定參數,應該使用rest參數...解決。
(4). 箭頭函數通過call和apply調用,不會改變this指向,只會傳入參數
let obj2 = {a: 10,b: function(n) {let f = (n) => n + this.a;return f(n);},c: function(n) {let f = (n) => n + this.a;let m = {a: 20};return f.call(m,n);} }; console.log(obj2.b(1)); // 11 console.log(obj2.c(1)); // 11(5). 箭頭函數沒有原型屬性
var a = ()=>{return 1; }function b(){return 2; }console.log(a.prototype); // undefined console.log(b.prototype); // {constructor: ?}(6). 箭頭函數不能作為Generator函數,不能使用yield關鍵字
(7). 箭頭函數返回對象時,要加一個小括號
var func = () => ({ foo: 1 }); //正確 var func = () => { foo: 1 }; //錯誤(8). 箭頭函數在ES6 class中聲明的方法為實例方法,不是原型方法
//deom1 class Super{sayName(){//do some thing here } } //通過Super.prototype可以訪問到sayName方法,這種形式定義的方法,都是定義在prototype上 var a = new Super() var b = new Super() a.sayName === b.sayName //true //所有實例化之后的對象共享prototypy上的sayName方法//demo2 class Super{sayName =()=>{//do some thing here } } //通過Super.prototype訪問不到sayName方法,該方法沒有定義在prototype上 var a = new Super() var b = new Super() a.sayName === b.sayName //false //實例化之后的對象各自擁有自己的sayName方法,比demo1需要更多的內存空間因此,在class中盡量少用箭頭函數聲明方法。
(9). 多重箭頭函數就是一個高階函數,相當于內嵌函數
const add = x => y => y + x; //相當于 function add(x){return function(y){return y + x;}; }(10). 箭頭函數常見錯誤
let a = {foo: 1,bar: () => console.log(this.foo) }a.bar() //undefinedbar函數中的this指向父作用域,而a對象沒有作用域,因此this不是a,打印結果為undefined
function A() {this.foo = 1 }A.prototype.bar = () => console.log(this.foo)let a = new A() a.bar() //undefined原型上使用箭頭函數,this指向是其父作用域,并不是對象a,因此得不到預期結果
?
?
參考: https://www.cnblogs.com/bfc0517/p/6706498.html
? ?https://www.cnblogs.com/biubiuxixiya/p/8610594.html
?
?
轉載于:https://www.cnblogs.com/Renyi-Fan/p/11600469.html
總結
以上是生活随笔為你收集整理的石川es6课程---4、箭头函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 心得体悟帖---15、我的灵魂
- 下一篇: 石川es6课程---5、函数-参数