【问题标题】:How can I recreate array methods?如何重新创建数组方法?
【发布时间】:2015-11-10 19:26:18
【问题描述】:

我正在尝试对数组方法 push、pull、shift、unshift 进行反向工程,但我似乎无法弄清楚如何 a) 构造它 b) 调用它。我怎么能这样做?以下是条件:

返回一个空数组对象。这个对象应该有 以下方法: push(val) 将 val 添加到数组的末尾 pop() 从末尾删除一个值并返回它 unshift(val) 添加 val 到数组的开头 shift() 从 开始并返回它这个问题的目标是扭转 设计数组方法实际在做什么并返回一个对象 有那些方法

这是我最初认为的样子。

function createArray() {
    //CODE HERE

  this.push = function Push(value){
                if(index >= 0){
                 Mainarray[index++]=value;}
               };

   this.pop = function (){
                if(index >= 0){
                  index--;
                 return Mainarray[index];
                }
                else{
                  // display message of Empty Array
                  console.log('Error: Item is not array');
                }
              };

   this.unshift = function(){return ;};
}

【问题讨论】:

    标签: javascript arrays methods


    【解决方案1】:

    你可以使用原型——像这样:

    function YourArray() {
      this.arr = [];
      this.index = 0;
    }
    
    YourArray.prototype.push = function( value ) {
      this.arr[ this.index++ ] = value;
      return this;
    }
    
    var arr = new YourArray();
    
    arr.push('foo');
    

    【讨论】:

      【解决方案2】:
      function NewArray() {
          this.array = [];
      }; /* Class */
      
      NewArray.prototype.push = function(data) {
          this.array.push(data);
      } /* Method */
      /* You should use prototypes, because all methods will became common, and if you are created methods like this.pop = function (){} then any instance will copy this functions */
      
      
      var n = new NewArray();
      n.push(2);
      console.log(n);
      

      Advantages of using prototype, vs defining methods straight in the constructor?

      【讨论】:

        【解决方案3】:

        您可以通过在相同数组长度的位置为数组分配一个值来重新创建推送方法。

        这是推送的原型:

        Array.prototype.push = function(element) {
            this[this.length] = element;
        };
        

        这是针对 pop 方法的:

        Array.prototype.pop = function() {
            var key = this.stack.pop();
            var prop = this.object[key];
            delete this.object[key];
            return prop;
        };
        

        您可以通过更改原型名称来创建自己的方法。 推送到 mypush 或其他东西

        推送函数 createArray 的示例:

        this.push = function pushValue(value) {
            this.arr[this.arr.length] = value;
        };
        

        【讨论】:

          【解决方案4】:

          我使用原生数组方法作为分配给返回对象中键的值。诀窍是在对象内声明一个数组并将其用作引用。它应该通过您正在寻找的检查。

          function createArray() {
              //CODE HERE
              return {
                 arr: [],
                 push: function (val) {this.arr.push(val)},
                 pop: function() {return this.arr.pop()},
                 unshift: function (val) {return this.arr.unshift(val)},
                 shift: function() {return this.arr.shift()}
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2014-01-22
            • 1970-01-01
            • 2015-07-28
            • 1970-01-01
            • 2015-12-19
            • 2010-10-26
            • 2022-01-20
            • 1970-01-01
            • 2019-01-17
            相关资源
            最近更新 更多