When Extending Array map is undefined

前端 未结 2 1517
南旧
南旧 2021-01-22 19:28

I\'m trying to extend Array so that I can add in my own additional functionality. I\'ve read that extending Array.prototype can be dangerous, so I am trying to do it the correct

2条回答
  •  执念已碎
    2021-01-22 20:00

    You used the spread operator on the wrong place. https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Operators/Spread_operator

    I guess you would like to reach more parameters into the constructor function.

            class Array2 extends Array {
              constructor(items) {
                super(items); //here is the problem
              }
    
              addOne() {
                return this.map( (x) => {
                  return x+1;
                } );
              }
            }
    
    
    
    
            let arry2 = Array2.from([2, 1, 4, 22]).addOne();
            console.log("arry2", arry2);

提交回复
热议问题