JavaScript参考手册
排列 | Array
array.copyWithin
copyWithin()
方法将数组的一部分简单地复制到同一数组中的另一个位置,并将其返回,而不修改其大小。
['alpha', 'bravo', 'charlie', 'delta'].copyWithin(2, 0);
// results in ["alpha", "bravo", "alpha", "bravo"]
语法
arr.copyWithin(target)
arr.copyWithin(target, start)
arr.copyWithin(target, start, end)
Parameters
target
0 为基底的索引,复制序列到该位置。如果是负数,target
将从末尾开始计算。如果target
大于等于arr.length
,将会不发生拷贝。如果target
在start
之后,复制的序列将被修改以符合arr.length
。
start
0 为基底的索引,开始复制元素的起始位置。如果是负数,start
将从末尾开始计算。如果 start
被忽略,copyWithin
将会从0开始复制。
end
0 为基底的索引,开始复制元素的结束位置。copyWithin
将会拷贝到该位置,但不包括end
这个位置的元素。如果是负数, end
将从末尾开始计算。如果end 被忽略,copyWithin
将会复制到arr.length
。
返回值
改变了的数组。
描述
copyWithin
就像 C 和 C++ 的 memcpy
函数一样,且它是用来移动Array
或者 TypedArray
数据的一个高性能的方法。复制以及粘贴序列这两者是为一体的操作;即使复制和粘贴区域重叠,粘贴的序列也会有拷贝来的值。
copyWithin
函数是设计为通用的,其不要求其this
值必须是一个数组对象。
The copyWithin
是一个可变方法,它不会改变 this
的长度,但是会改变 this
本身的内容,且需要时会创建新的属性。
示例
[1, 2, 3, 4, 5].copyWithin(-2);
// [1, 2, 3, 1, 2]
[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(-2, -3, -1);
// [1, 2, 3, 3, 4]
[].copyWithin.call({length: 5, 3: 1}, 0, 3);
// {0: 1, 3: 1, length: 5}
// ES2015 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 ES2015 compliant:
[].copyWithin.call(new Int32Array([1, 2, 3, 4, 5]), 0, 3, 4);
// Int32Array [4, 2, 3, 4, 5]
Polyfill
if (!Array.prototype.copyWithin) {
Array.prototype.copyWithin =
// Array: Number[, Number[, Number]]
function copyWithin(target, start, stop) {
var positiveT = target >= 0,
positiveS = (start = start | 0) >= 0,
length = this.length,
zero = 0,
r = function() {return ((+new Date) * Math.random()).toString(36)},
delimiter = "\b" + r() + "-" + r() + "-" + r() + "\b",
hold;
stop = stop || this.length;
hold = this.slice.apply(this,
positiveT?
[start, stop]:
positiveS?
[start, -target]:
[start])
.join(delimiter);
return this.splice.apply(this,
positiveT?
[target, stop - start, hold]:
positiveS?
[target, stop, hold]:
[target, start, hold]),
this.join(delimiter).split(delimiter).slice(zero, length);
}
}
规范
Specification |
Status |
Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Array.prototype.copyWithin' in that specification. |
Standard |
Initial definition. |
ECMAScript 2016 (ECMA-262)The definition of 'Array.prototype.copyWithin' in that specification. |
Standard |
|
ECMAScript Latest Draft (ECMA-262)The definition of 'Array.prototype.copyWithin' in that specification. |
Living Standard |
|
浏览器支持
Feature |
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
---|---|---|---|---|---|---|
Basic Support |
45 |
12 |
32 |
未实现 |
32 |
9 |
Feature |
Android |
Chrome for Android |
Edge mobile |
Firefox for Android |
IE mobile |
Opera Android |
iOS Safari |
---|---|---|---|---|---|---|---|
Basic Support |
(已实现) |
(已实现) |
(已实现) |
32 |
未实现 |
(已实现) |
(已实现) |
排列 | Array相关

JavaScript 是一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于原型)的解释型语言。它已经由ECMA(欧洲电脑制造商协会)通过 ECMAScript 实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器( Chrome、IE、FireFox、Safari、Opera )支持。JavaScript 是一门基于原型、函数先行的语言,是一门多范式的语言,它支持面向对象编程,命令式编程,以及函数式编程。它提供语法来操控文本、数组、日期以及正则表达式等,不支持 I/O,比如网络