首页 > 其他 > 详细

[ES6] Rest Parameter

时间:2016-01-02 22:32:08      阅读:197      评论:0      收藏:0      [点我收藏+]

Problem with the ES5:

function displayTags(){
    for (let i in arguments) {
        let tag = arguments[i];
        _addToTopic(tag);
    }
}
  • Hard to tell which parameters this functon expects to be called with
  • arguments -- where did this come from?
  • IF we add an agument, it will break everything:
function displayTags(targetElement){

    let target = _findElement(targetElement);

    for (let i in arguments) {
        let tag = arguments[i]; // break the loop, since the first arguments is no longer a tag
        _addToTopic(target, tag);
    }
}

 

 

Improvement from ES6:

// Cannot assign default value to Rest Parameter
// Rest Parameter should alwasys come at the last
const displayTags = (blogName="New Blog", ...tags) => {
  console.log(blogName, tags);
}

displayTags("ES2015", "Javascript", "ES6", "Babel");

 

[ES6] Rest Parameter

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

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