• 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. Windows网络服务渗透攻击分类

    网络服务渗透攻击分为三类 一.针对于windows系统自带的网络服务的渗透攻击 1.针对于NetBIOS的攻击 NetBIOS以运行在TCP/IP系统中的NBT协议来实现,具体包括在UDP的137端口 ...

  2. header请求头信息详细介绍

    https://www.byvoid.com/zhs/blog/http-keep-alive-header HTTP协议头部与Keep-Alive模式详解 1.什么是Keep-Alive模式? 我们 ...

  3. Linux快速查看某条命令的版本和存放的位置(ls -l `which mvn`)

    输入: ls -l `which mvn` 如图:

  4. Visual Studio 跨平台開發實戰(3) - Xamarin iOS 多頁面應用程式開發 (转帖)

    前言 在前一篇教學中, 我們學會如何使用Visual Studio 搭配Xcode 進行iOS基本控制項的操作. 但都是屬於單一畫面的應用程式. 這次我們要來練習如何透過Navigation Cont ...

  5. linux安装oracle 报错[INS-20802] Oracle Net Configuration Assistant failed 解决办法

    [INS-20802] Oracle Net Configuration Assistant failed 首先从LinuxIDC.com下载这个补丁包,然后用 unzip p8670579_1120 ...

  6. 使用grunt搭建自动化的web前端开发环境

    使用grunt搭建自动化的web前端开发环境 我们一定经常听过grunt和gulp,它们都是用于搭建自动化的web前端开发环境的,这里主要介绍grunt的使用,值得一提的是,jQuery.bootst ...

  7. Jmeter之测试报告

    当我们完成测试后,需要通过报告来查看测试结果 一.聚合报告 Label:每个JMeter的element的Name值.例如HTTP Request的Name #Samples:发出请求数量.例如:如第 ...

  8. Perl入门

    Perl 是一门开源的脚本语言,由 Larry Wall 所创造,该语言以实用,快速开发为主要目标,与当前流行的面向对象结构化编程有些格格不入,但这并不妨碍 Perl 被广泛流传和使用,世界范围内围绕 ...

  9. c#删除list中的元素

    public static void TestRemove() { string[] str = { "1", "2", "d", &quo ...

  10. Vue组件库

    滴滴cube-ui https://didi.github.io/cube-ui/#/zh-CN/docs/quick-start 有赞开源Vant(适合做商城) https://tech.youza ...