• [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");
  • 相关阅读:
    css常用属性记录
    js字符串常用方法总结
    mongoose基本操作
    本地存储API
    历史相关API
    自定义播放器
    HTML5全屏操作API
    HTML5自定义属性操作
    HTML5类操作
    案例:3D切割轮播图
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5095361.html
Copyright © 2020-2023  润新知