[ES6] Rest Parameter

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");
上一篇:Git是目前世界上最先进的分布式版本控制系统


下一篇:BOM之history对象