2017-09-29 11 views
0

私はmyarray.length = 0メソッドを使用して、カスタムArray.prototype.clear()メソッドで配列を切り捨てています。長さを0に設定して配列を切り詰めると、JavaScriptが正しく動作しない

私の知る限り、これまでのところうまくいきましたが、length0に設定している特定のケースがありますが、まだ要素が残っています。

私は基本的なアレイテストを実行しましたが、動作するようですが、このより複雑なケースではそうではないので、問題を診断するのは難しいです。ここで

Object.defineProperty(Array.prototype, 'clear', { 
    value: function clear() { 
     // Clear the array using the length = 0 method. See the method's comments on links to more information and benchmarks on this technique. 
     this.length = 0; 
    }, 
    enumerable: false, 
    configurable: true, 
    writable: false 
}); 

私は他のすべての場合に、それを呼び出すと、次のような作品見事に...私が使用している私のArray.prototypeの延長コードです。

var myarray = [ 1,2,3,4,5,6,6,7,8,9, new Date() ]; 
myarray.clear(); 
console.log(myarray); // all cleared. { length: 0 } 

しかし、配列は約10〜20の要素を持っていると私は、アレイ上.clear()を呼び出し、それが0に長さを設定しますが、私はのような配列をconsole.log場合の要素はまだ見ることができます私のより複雑な場合私は上記をしました。

// e.g. { 0: ..., 1: ..., 2: ..., 3: ..., 4: ..., length: 0 } 

唯一の違いは、私が使用していますArrayオブジェクトがコールオーバーライドされた機能を実行し、オーバーライド、と、オブジェクトを拡張していることです。そのようなものは...

function Collection() { Array.apply(this); } 
Collection.prototype = Object.create(Array.prototype, { 
    push: { 
     enumerable: false, 
     value: function(item) { 
      // ...does a type checks on 'item' 
      Array.prototype.push.apply(this, [ item ]); 
     } 
    }, 
    clear: { 
     enumerable: false, 
     value: function() { Array.prototype.clear.apply(this); } 
    } 
}); 

これは、Chromeで起こっている、と私は私のテストをやったとして、Array.prototype.clear方法にスルーそれを強化しているが、いくつかの理由で、第2の場合には動作しません。

アイデア?

+3

に明確な追加避けることができますを使用する場合は、yのように思えますオブジェクト上でそれを試してみると、オブジェクトの長さは別のプロパティになり、オブジェクトの内容は変更されません。ネイティブプロトタイプを拡張することは一般的には悪いことです。 – adeneo

+0

ネイティブを拡張することは悪い考えではありません...「配列」を除いて - それは他のネイティブに比べて「魔法の力」を持っています –

+0

@JaromandaX - それは常に悪い考えではありませんが、一般的には悪い考えです。つまり、この場合、プロトタイプに触れることなく、通常の 'clear(Array) 'が同じことをするのを避けるべきです。少なくともそれは列挙できません。 – adeneo

答えて

2

新しいES2015 + class

Object.defineProperty(Array.prototype, 'clear', { 
 
     value: function clear() { 
 
      this.length = 0; 
 
     }, 
 
     enumerable: false, 
 
     configurable: true, 
 
     writable: false 
 
    }); 
 

 
    class Collection extends Array { 
 
     constructor(...args) { 
 
      super(...args); 
 
     } 
 
     push(...args) { 
 
      console.log('overridden push', ...args); 
 
      // only push even values for example 
 
      super.push(...args.filter(x=>x%2 == 0)); 
 
     } 
 
    } 
 

 
    var x = new Collection(); 
 
    x.push(1,2,3,4); 
 
    console.log('after push', JSON.stringify(x)); 
 
    x.clear(); 
 
    console.log('after clear', JSON.stringify(x));

か、配列

class Collection extends Array { 
 
     constructor(...args) { 
 
      super(...args); 
 
     } 
 
     push(...args) { 
 
      console.log('overridden push', ...args); 
 
      // only push even values for example 
 
      super.push(...args.filter(x=>x%2 == 0)); 
 
     } 
 
     clear() { 
 
      this.length = 0; 
 
     } 
 
    } 
 

 
    var x = new Collection(); 
 
    x.push(1,2,3,4); 
 
    console.log('after push', JSON.stringify(x)); 
 
    x.clear(); 
 
    console.log('after clear', JSON.stringify(x));

+0

ありがとう@JaromandaX。私はES2015 +が好きで、それを使うのが好きです...悲しいかなかブラウザ(後ろ向き)のサポートはまだまだ完全にはありません*一息*。 @adeneoが述べたように、Objectであることと何か関係があるのだろうかと思います。トリックを行うと思われる 'Array.prototype.splice()'メソッドを使用しました。 –

関連する問題