★ wanayoo — archive 1999 https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithinNouvelle recherche | Portail wanayoo
mozilla
Your Search Results

    Array.prototype.copyWithin()

    This article is in need of an editorial review.

    This translation is incomplete. Please help translate this article from English.

    これは Harmony(ECMAScript 6) 提案の一部であり、実験段階の技術です。
    この技術の仕様は安定していません。ブラウザ互換性の一覧表を確認してください。またこれらの構文や動作は、仕様変更などにより、新しいバージョンのブラウザでは変更される可能性があるという点に注意してください。

    Summary

    配列内の要素をtargetで始まる位置にコピーします。 引数start と endの位置からコピーされます。end引数はオプションでデフォルトで配列の最後尾となります。

    構文

    arr.copyWithin(target, start[, end = this.length])

    引数

    target
    要素コピー先開始位置
    start
    要素コピー元開始位置
    end
    オプション。要素コピー元終了位置

    説明

    引数target, start, endは Numberに強いられて、integerに切り縮められます。

    startがマイナスなら、length+startとして扱われます。length は、配列の要素数です。endがマイナスなら、 length+endとして、扱われます。

    copyWithin関数は故意にgenericです。this値がArrayオブジェクトであることを必要としません。 加えて、copyWithin は mutable methodです。thisオブジェクト自身を変更し、コピーではなく、オブジェクト自身を返します。

    例

    [1, 2, 3, 4, 5].copyWithin(0, 3);
    // [4, 5, 3, 4, 5]
    
    [1, 2, 3, 4, 5].copyWithin(0, 3, 4);
    // [4, 2, 3, 4, 5]
    
    [1, 2, 3, 4, 5].copyWithin(0, -2, -1);
    // [4, 2, 3, 4, 5]
    
    [].copyWithin.call({length: 5, 3: 1}, 0, 3);
    // {0: 1, 3: 1, length: 5}
    
    // ES6 Typed Arrays are subclasses of Array
    var i32a = new Int32Array([1, 2, 3, 4, 5]);
    
    i32a.copyWithin(0, 2);
    // Int32Array [3, 4, 5, 4, 5]
    
    // On platforms that are not yet ES6 compliant: 
    [].copyWithin.call(new Int32Array([1, 2, 3, 4, 5]), 0, 3, 4);
    // Int32Array [4, 2, 3, 4, 5]
    

    互換性

    if (!Array.prototype.copyWithin) {
      Array.prototype.copyWithin = function(target, start/*, end*/) {
        // Steps 1-2.
        if (this == null) {
          throw new TypeError('this is null or not defined');
        }
    
        var O = Object(this);
    
        // Steps 3-5.
        var len = O.length >>> 0;
    
        // Steps 6-8.
        var relativeTarget = target >> 0;
    
        var to = relativeTarget < 0 ?
          Math.max(len + relativeTarget, 0) :
          Math.min(relativeTarget, len);
    
        // Steps 9-11.
        var relativeStart = start >> 0;
    
        var from = relativeStart < 0 ?
          Math.max(len + relativeStart, 0) :
          Math.min(relativeStart, len);
    
        // Steps 12-14.
        var end = arguments[2];
        var relativeEnd = end === undefined ? len : end >> 0;
    
        var final = relativeEnd < 0 ?
          Math.max(len + relativeEnd, 0) :
          Math.min(relativeEnd, len);
    
        // Step 15.
        var count = Math.min(final - from, len - to);
    
        // Steps 16-17.
        var direction = 1;
    
        if (from < to && to < (from + count)) {
          direction = -1;
          from += count - 1;
          to += count - 1;
        }
    
        // Step 18.
        while (count > 0) {
          if (from in O) {
            O[to] = O[from];
          } else {
            delete O[to];
          }
    
          from += direction;
          to += direction;
          count--;
        }
    
        // Step 19.
        return O;
      };
    }
    

    仕様

    仕様 ステータス コメント
    ECMAScript 6 (ECMA-262)
    The definition of 'Array.prototype.copyWithin' in that specification.
    勧告候補 Initial definition.

    ブラウザ実装状況

    機能 Chrome Firefox (Gecko) Internet Explorer Opera Safari
    基本サポート 未サポート 32 (32) 未サポート 未サポート 未サポート
    機能 Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
    基本サポート 未サポート 未サポート 32.0 (32) 未サポート 未サポート 未サポート

    関連情報

    ドキュメントのタグと貢献者

    Contributors to this page: shide55
    最終更新者: shide55,
    サイドバーを隠す