javascript數(shù)組克隆簡單實現(xiàn)方法
本文實例講述了javascript數(shù)組克隆簡單實現(xiàn)方法。分享給大家供大家參考,具體如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建網(wǎng)頁 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>
腳本之家小編補充
The JavaScript
To clone the contents of a given array, all you need to do is call slice, providing 0 as the first argument:
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);
};
And there you have it! Don't iterate over arrays to clone them if all you need is a naive clone!
希望本文所述對大家JavaScript程序設計有所幫助。
相關文章
highcharts.js數(shù)據(jù)綁定方式代碼實例
這篇文章主要介紹了highcharts.js數(shù)據(jù)綁定方式代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11
Electron去掉窗口邊框并添加關閉按鈕的實現(xiàn)步驟
在?Electron?中,如果你想去掉默認的窗口邊框(frame)并添加額外的按鍵,可以通過相關步驟實現(xiàn),下面小編給大家?guī)砹薊lectron去掉窗口邊框并添加關閉按鈕的實現(xiàn)步驟,感興趣的朋友一起看看吧2024-06-06
Bootstrap的popover(彈出框)在append后彈不出(失效)
這篇文章主要介紹了Bootstrap的popover(彈出框)在append后彈不出,失效的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02
Javascript 拖拽的一些簡單的應用(逐行分析代碼,讓你輕松了拖拽的原理)
這篇文章主要介紹了Javascript 拖拽的一些簡單的應用(逐行分析代碼,讓你輕松了拖拽的原理),需要的朋友可以參考下2015-01-01
javascript add event remove event

