JavaScript笔记-箭头函数
https://www.liaoxuefeng.com/wiki/1022910821149312基于廖雪峰老师的教程所做的笔记。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>箭头函数操作展示</title> </head> <body>
<script>
function square(x){ return x*x; } var squareArrow=x=>x*x;
console.log(square(2)); console.log(squareArrow(2));
function add(x,y){ return x+y; } var addArrow=(x,y)=>x+y;
console.log(add(1,2)); console.log(addArrow(1,2));
function getName(){ return{ firstName:"Guorui", lastName:"Sang" } } var getNameArrow=()=>({ firstName: "Guorui", lastName: "Sang" })
console.log(getName()); console.log(getNameArrow());
function abs(x){ if(x>=0) return x; else return -x; }
var absArrow=(x)=>{ if(x>=0) return x; else return -x; }
console.log(abs(-11)); console.log(absArrow(-11));
var obj={ age:20, city:"Shenzhen", show:function(){ console.log(this.age); let printCity=()=>{ console.log(this.city); } printCity(); } }
obj.show();
</script> </body> </html>
|