javascript数组克隆简单实现方法

本文实例讲述了javascript数组克隆简单实现方法。分享给大家供大家参考,具体如下:文章来源地址:https://www.yii666.com/blog/115955.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建网页 1</title>
</head>
<body>
<script language=javascript>
var a = ['a','b','c','d','e','f'];
var b = a.concat();
b.push('test is ok!');
alert(b.join(','));
alert(a.join(','));
</script>
</body>
</html>

yii666小编补充文章来源地址https://www.yii666.com/blog/115955.html

The JavaScript
To clone the contents of a given array, all you need to do is call slice, providing 0 as the first argument:网址:yii666.com

var clone = myArray.slice(0);

The code above creates clone of the original array; keep in mind that if objects exist in your array, the references are kept; i.e. the code above does not do a "deep" clone of the array contents. To add clone as a native method to arrays, you'd do something like this:

Array.prototype.clone = function() {
return this.slice(0);
};网址:yii666.com<

And there you have it! Don't iterate over arrays to clone them if all you need is a naive clone!

希望本文所述对大家JavaScript程序设计有所帮助。文章地址https://www.yii666.com/blog/115955.html

版权声明:本文内容来源于网络,版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。文本页已经标记具体来源原文地址,请点击原文查看来源网址,站内文章以及资源内容站长不承诺其正确性,如侵犯了您的权益,请联系站长如有侵权请联系站长,将立刻删除

领支付宝红包赞助服务器费用

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信图片_20190322181744_03.jpg

微信扫一扫打赏

请作者喝杯咖啡吧~

支付宝扫一扫领取红包,优惠每天领

二维码1

zhifubaohongbao.png

二维码2

zhifubaohongbao2.png