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程序設(shè)計有所幫助。
相關(guān)文章
原生JS實現(xiàn)列表子元素順序反轉(zhuǎn)的方法分析
這篇文章主要介紹了原生JS實現(xiàn)列表子元素順序反轉(zhuǎn)的方法,結(jié)合實例形式分析了javascript針對dom元素、數(shù)組reverse方法、innerHTML方法等列表元素順序翻轉(zhuǎn)相關(guān)操作技巧,需要的朋友可以參考下2018-07-07JavaScript組件焦點與頁內(nèi)錨點間傳值的方法
這篇文章主要介紹了JavaScript組件焦點與頁內(nèi)錨點間傳值的方法,涉及輸入框與錨點的操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-02-02Express框架Router?Route?Layer對象使用示例詳解
這篇文章主要為大家介紹了Express框架Router?Route?Layer對象使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03