码迷,mamicode.com
首页 > 编程语言 > 详细

javascript箭头函数

时间:2017-09-23 10:47:24      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:turn   syntax   syn   creat   his   https   参数   lin   calling   

原文 https://thewebjuice.com/es6-arrows/

1 使用es6箭头定义匿名函数

(msg)=>console.log(‘Hello World‘)

es5

‘use strict‘;

(function (msg) {
  return console.log(‘Hello World‘);
});

2 单个参数和多个参数

// Multiple Parameter
(arg1,arg2,arg3,arg4)=>{
  return arg1+arg2+arg3+arg4
}

// Single Parameter 
(arg1)=>{ 
return arg1
}

es5

"use strict";

// Multiple Parameter
(function (arg1, arg2, arg3, arg4) {
  return arg1 + arg2 + arg3 + arg4;
});

// Single Parameter 
(function (arg1) {
  return arg1;
});

 

3定义闭包

 

 1 // Single Line Closure
 2 var SayHello=(hello)=>console.log(hello)
 3 
 4 // Multi Line Closure
 5 var SayHelloAgain=(hello)=>{
 6 console.log(This is a multiline Closure)
 7 console.log(hello)
 8 }
 9 
10 // Calling the Two above Closure
11 SayHello(Hey I am ES6 Arrow)
12 SayHelloAgain(Heya Again!!!);

 

 

es5

 1 use strict;
 2 
 3 // Single Line Closure
 4 var SayHello = function SayHello(hello) {
 5   return console.log(hello);
 6 };
 7 
 8 // Multi Line Closure
 9 var SayHelloAgain = function SayHelloAgain(hello) {
10   console.log(This is a multiline Closure);
11   console.log(hello);
12 };
13 
14 // Calling the Two above Closure
15 SayHello(Hey I am ES6 Arrow);
16 SayHelloAgain(Heya Again!!!);

 

4  Literal Syntax 

1 var createObject = (x,y,color)=>({x:x,y:y,z:z})

 

es5 

1 "use strict";
2 
3 var createObject = function createObject(x, y, color) {
4   return { x: x, y: y, z: z };
5 };

 

javascript箭头函数

标签:turn   syntax   syn   creat   his   https   参数   lin   calling   

原文地址:http://www.cnblogs.com/or2-/p/7580153.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!