JavaScript多态

function Polymorphism() {
    this.showByParamNums = (...args) => {
        const argsLen = args.length;
        switch(argsLen) {
            case 0:
                return (() => {
                  return '0 param';
                })();
            case 1:
                return ((oneParam) => {
                  return `1 param: ${oneParam}`;
                })(...args);
            case 2:
                return ((oneParam, twoParam) => {
                  return `2 params: ${oneParam}, ${twoParam}`;
                })(...args);
            default:
              return (() => {
                return 'other';
              })();
        }
    }
}

const p = new Polymorphism();
console.log(p.showByParamNums());
console.log(p.showByParamNums('one'));
console.log(p.showByParamNums('one', 'two'));
console.log(p.showByParamNums('one', 'two', 'three'));

你可能感兴趣的:(前端开发,前端,JavaScript,ES6,前端笔记)