首页 > 其他 > 详细

[ES6] 07. Default Value for function param

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!