• JavaScript : Array assignment creates reference not copy

29 May 2015

Consider we have an array var a = [1,2,3,4]; and we assign var b = a; then b not a copy of a, b is a pointer to a. So if you make any changes on b will have effect on a as well. Here is an example.

var a = [1,2,3,4];
var b = a;
var b.pop();
console.log(b); // [1, 2, 3]
console.log(a); // [1, 2, 3]

so if you really want a copy of a you need to use .slice method.

var a = [1,2,3,4];
var b = a.slice(0);
var b.pop();
console.log(b); // [1, 2, 3]
console.log(a); // [1, 2, 3, 4]

JavaScript doesn’t have a clone method for array, so if you really want, you can define yourself on Array but its not highly recommended.

Array.prototype.clone = function(){
return this.slice(0)
}

Now you can call .clone on any Array type.

var a = [1,2,3,4];
var b = a.clone();
var b.pop();
console.log(b); // [1, 2, 3]
console.log(a); // [1, 2, 3, 4]

I recommend to keep this in mind when ever you are dealing with JavaScript Array’s, else you might facing some unexpected behaviour and wonder why it happens.

Happy coding.

Share this on Twitter | Facebook | Google+
If you particularly enjoy my work, I appreciate donations via PayPal.

JavaScript : Array assignment creates reference not copy的更多相关文章

  1. [Javascript] Array methods in depth - slice

    Array slice creates a shallow copy of an array. In this lesson we cover, in detail, exactly what a ' ...

  2. [Javascript] Array methods in depth - filter

    Array filter creates a new array with all elements that pass the test implemented by the provided fu ...

  3. javascript array操作

    首先来看一下怎么判断一个对象是不是数组: 1.Array.isArray(obj) 调用数组的isArray方法 2.obj instanceof Array 判断对象是否是Array的实例 3.Ob ...

  4. JavaScript Array methods performance compare

    JavaScript Array methods performance compare JavaScript数组方法的性能对比 env $ node -v # v12.18.0 push vs un ...

  5. JavaScript Array 对象

    JavaScript Array 对象 Array 对象 Array 对象用于在变量中存储多个值: var cars = ["Saab", "Volvo", & ...

  6. JavaScript Array(数组)对象

    一,定义数组 数组对象用来在单独的变量名中存储一系列的值. 创建 Array 对象的语法: new Array(); new Array(size); new Array(element0, elem ...

  7. call by reference and copy/restore

    转自:http://stackoverflow.com/questions/8848402/whats-the-difference-between-call-by-reference-and-cop ...

  8. Javascript Array.prototype.some()

    当我们使用数组时,查找数组中包含某个特殊的项是非常常见的动作.下面例子是一个简单的实现: 01 planets = [ 02     "mercury", 03     " ...

  9. [Javascript ] Array methods in depth - sort

    Sort can automatically arrange items in an array. In this lesson we look at the basics including how ...

随机推荐

  1. js继承(自备水,这非常干货)

    讲js继承之前,想一想什么是继承? 生活中有很多例子,比方说继承财产,继承女朋友的前男友的前女友 ヽ(ー_ー)ノ ,这些和js继承差不多,但是有一个不一样的地方,就是继承过后,原先的人就没有了,js继 ...

  2. 微信 oauth 登录 ,回调两次,一个坑,记录一下。

    在做微信某个功能的时候,大致需求是:静默授权,得到openId ,然后拿着openId调用接口,判断是否关注.如果是关注的,则发放礼券.每个我网站的会员只会发放一次礼券.如果第二次则会提示已领取过礼券 ...

  3. 发布aar到jcenter

    准备工作 创建bintray账号; 在https://bintray.com/, 选择如下图中,方框内"Sign Up Here": 选择合适的方式,创建账号: 新建仓库: Add ...

  4. 【Qt开发】常用控件--QSpinBox和QDoubleSpinBox

    QSpinBox和QDoubleSpinBox 是UI设计常用的控件. QSpinBox可用于显示和输入整数,并可以在显示框中添加前缀或后缀. QDoubleSpinBox可用于显示和输入小数,并可以 ...

  5. 使用单体模式设计原生js插件

    ----------基于上次写的jquery插件进行改造  http://www.cnblogs.com/GerryOfZhong/p/5533773.html 背景:jQuery插件依赖jQuery ...

  6. docker搭建gitlab,设置邮件提醒,并运行runner

    接着http://www.cnblogs.com/wsy1030/p/8431837.html 在另一台机子运行gitlab: docker run --name='gitlab' -d -p 222 ...

  7. ansible api常用模块与参数

    ###ansibleAPI 常用模块 用于读取yaml,json格式的文件 from ansible.parsing.dataloader import DataLoader #用于管理变量的类,包括 ...

  8. 很实用的web性能测试插件:Yslow , PageSpeed

    package org.springframework.web.servlet.resource; import java.io.IOException; import java.io.Unsuppo ...

  9. shell通过ping检测整个网段IP的网络状态脚本

    要实现Ping一个网段的所有IP,并检测网络连接状态是否正常,很多方法都可以实现,下面简单介绍两种,如下:脚本1#!/bin/sh# Ping网段所有IP# 2012/02/05ip=1 #通过修改初 ...

  10. mac terminal中快捷移动光标 持续更新。。。

    1.option + ←/→ 以单词为单位快速移动 2.ctrl + A 移动到行首 3.ctrl + B 移动到行尾 4.ctrl + K 删除光标后至行尾的内容