Ways to extend Array object in javascript

前端 未结 8 2290
[愿得一人]
[愿得一人] 2020-12-13 13:02

i try to extend Array object in javascript with some user friendly methods like Array.Add() instead Array.push() etc...

i implement 3 ways to do this. unfortunetly t

8条回答
  •  误落风尘
    2020-12-13 13:13

    You can also use this way in ES6:

    Object.assign(Array.prototype, {
        unique() {
          return this.filter((value, index, array) => {
            return array.indexOf(value) === index;
          });
        }
    });
    

    Result:

    let x = [0,1,2,3,2,3];
    let y = x.unique();
    console.log(y); // => [0,1,2,3]
    

提交回复
热议问题