码迷,mamicode.com
首页 > 其他好文 > 详细

[ES6] 07. Default Value for function param

时间:2014-11-20 06:48:53      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   sp   for   on   div   

Normally, we can set default value for function param:

//Here use "Hello" as default param
var receive =function(message="Hello", handle){
    handler(message);
}

receive("Come", function(message){
   console.log(message + ", "+ "John");
});

 

What we can do is use function as a default param:

var receive =function(message="Hello", handler=function(message){
    console.log(message + ", "+ "John");
}){
    handler(message);
}

receive("Come");  //Come, John

 

Then we can use => to refactor the code:

var receive =function(message="Hello", handler= message => console.log(message + ", "+ "John")){
    handler(message);
}

receive("Go");  //Go, John

 

It will be crazy: (do not use this, cannot be understood)

let receive = (message="Hello", handler= message => console.log(message + ", "+ "John")) => handler(message)

receive(); //Hello John

 

[ES6] 07. Default Value for function param

标签:style   blog   io   ar   color   sp   for   on   div   

原文地址:http://www.cnblogs.com/Answer1215/p/4109677.html

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