JavaScript数组的快速克隆

JavaScript中对变量的操作都是通过引用方式,而对数组也一样。
前两天想要对一个数组进行复制,一直苦于找不到办法(遍历的方法俺是绝对不采用的),最后到blueidea上发问,得到的方法还是好麻烦……

无意中拿起《JavaScript权威指南》翻了翻数组的操作函数,发现了slice()函数。

slice()原来是用来截取数组中的一部分,这里我用它来复制数组,它的格式如下:
array.slice(start, end)
如果省略了end参数,则切分的数组包含从start开始到数组结束的所有元素。

现在要用它来复制数组,就一行,呵呵:
var newArray=oldArray.slice(0);

小小意思,不成敬意,要是你觉得它好用的话就拿去用吧 :)

— EOF —

《JavaScript数组的快速克隆》有27个想法

  1. <script type="text/javascript">
    var a = [1,2,3];
    var b = a.slice(0);
    a[0] = 4;
    alert(a); // [4,2,3]
    alert(b); // [1,2,3]
    </script>

    我试了,结果不是引用,谢谢!

  2. It is actually a great and helpful piece of info. I am satisfied that you just shared this useful info with us. Please stay us up to date like this. Thank you for sharing.

  3. Great tremendous issues here. I am very satisfied to peer your post. Thank you so much and i am looking forward to touch you. Will you please drop me a mail?

  4. I simply wished to say thanks all over again. I do not know the things that I would’ve undertaken without the suggestions provided by you over such a topic. It seemed to be a traumatic setting in my opinion, but observing the professional tactic you handled it made me to weep with joy. I will be happy for this help and as well , believe you comprehend what a powerful job your are accomplishing educating men and women through a blog. Most likely you have never met any of us.

  5. Pretty component of content. I simply stumbled upon your weblog and in accession capital to claim that I get in fact loved account your weblog posts. Any way I’ll be subscribing to your feeds and even I success you get entry to persistently fast.|

  6. Junge verdorbene 18jährige Teenys wollen noch viele Erfahrungen mit älteren Männern sammeln, bei uns kannst du sie kennen lernen.

  7. One thing I have actually noticed is that often there are plenty of fallacies regarding the banking companies intentions whenever talking about property foreclosures. One fable in particular is always that the bank wishes to have your house. The lending company wants your hard earned money, not the home. They want the funds they loaned you having interest. Keeping away from the bank will simply draw the foreclosed summary. Thanks for your write-up.

  8. back to basics garcinia cambogia forskolin hoaxbuster does diet works green coffee bean extract work select greens 10 day acai detox mangosteen paradise lost hoodia gordonii holland and barrett products liability now psyllium husk powder bulk buy online pick up in store mastic gum properties sensa nutrition facts glucomannan okra seeds

  9. These postures are not just poses or the types that you may have seen aged Chinese individuals practicing in parks. Do what ever it takes to endure and avoid becoming violated. Some individuals will go as much as performing out.

  10. Hello, you used to write wonderful, but the last few posts have been kinda boring… I miss your super writings. Past several posts are just a little bit out of track! come on!

  11. Generally I don’t read post on blogs, but I would like to say that this write-up very forced me to try and do so! Your writing style has been surprised me. Thanks, quite nice article.

发表评论

电子邮件地址不会被公开。 必填项已用*标注