Is JavaScript a pass-by-reference or pass-by-value language?

A very detailed explanation about copying, passing and comparing by value and by reference is in this chapter of the "JavaScript: The Definitive Guide" book.

Before we leave the topic of manipulating objects and arrays by reference, we need to clear up a point of nomenclature.

The phrase "pass by reference" can have several meanings. To some readers, the phrase refers to a function invocation technique that allows a function to assign new values to its arguments and to have those modified values visible outside the function. This is not the way the term is used in this book.

Here, we mean simply that a reference to an object or array -- not the object itself -- is passed to a function. A function can use the reference to modify properties of the object or elements of the array. But if the function overwrites the reference with a reference to a new object or array, that modification is not visible outside of the function.

Readers familiar with the other meaning of this term may prefer to say that objects and arrays are passed by value, but the value that is passed is actually a reference rather than the object itself.

Javascript by reference vs. by value [duplicate]

My understanding is that this is actually very simple:

  • Javascript is always pass by value, but when a variable refers to an object (including arrays), the "value" is a reference to the object.
  • Changing the value of a variable never changes the underlying primitive or object, it just points the variable to a new primitive or object.
  • However, changing a property of an object referenced by a variable does change the underlying object.

So, to work through some of your examples:

function f(a,b,c) {
// Argument a is re-assigned to a new value.
// The object or primitive referenced by the original a is unchanged.
a = 3;
// Calling b.push changes its properties - it adds
// a new property b[b.length] with the value "foo".
// So the object referenced by b has been changed.
b.push("foo");
// The "first" property of argument c has been changed.
// So the object referenced by c has been changed (unless c is a primitive)
c.first = false;
} var x = 4;
var y = ["eeny", "miny", "mo"];
var z = {first: true};
f(x,y,z);
console.log(x, y, z.first); // 4, ["eeny", "miny", "mo", "foo"], false

Example 2:

var a = ["1", "2", {foo:"bar"}];
var b = a[1]; // b is now "2";
var c = a[2]; // c now references {foo:"bar"}
a[1] = "4"; // a is now ["1", "4", {foo:"bar"}]; b still has the value
// it had at the time of assignment
a[2] = "5"; // a is now ["1", "4", "5"]; c still has the value
// it had at the time of assignment, i.e. a reference to
// the object {foo:"bar"}
console.log(b, c.foo); // "2" "bar"

实战

Is JavaScript a pass-by-reference or pass-by-value language?的更多相关文章

  1. Does Java pass by reference or pass by value?(Java是值传递还是引用传递) - 总结

    这个话题一直是Java程序员的一个热议话题,争论不断,但是不论是你百度搜也好还是去看官方的文档中所标明的也好,得到的都只有一个结论:Java只有值传递. 在这里就不贴代码细致解释了,让我们来看看一些论 ...

  2. 值传递:pass by value(按值传递) 和 pass by reference(引用传递)-[all]-[编程原理]

    所有的编程语言,都会讨论值传递问题. 通过一个js示例直观认识 //理解按值传递(pass by value)和按引用传递(pass by reference) //pass by value var ...

  3. JavaScript : Array assignment creates reference not copy

    JavaScript : Array assignment creates reference not copy 29 May 2015 Consider we have an array var a ...

  4. [GeekBand ] 利用 pass by reference -to -const 编写高效规范的 c++代码

    本文参考资料 :  GeekBand 侯捷老师,学习笔记 Effective C ++ 侯捷译 条款20 开发环境采用:VS2013版本 首先:分析值传递的缺点 (一) class Person{ p ...

  5. 学习OpenStack之 (4): Linux 磁盘、分区、挂载、逻辑卷管理 (Logical Volume Manager)

    0. 背景: inux用户安装Linux操作系统时遇到的一个常见的难以决定的问题就是如何正确地评估各分区大小,以分配合适的硬盘空间.普通的磁盘分区管理方式在逻辑分区划分好之后就无法改变其大小,当一个逻 ...

  6. Linux 磁盘管理

    Linux磁盘管理好坏管理直接关系到整个系统的性能问题. Linux磁盘管理常用三个命令为df.du和fdisk. df:列出文件系统的整体磁盘使用量 du:检查磁盘空间使用量 fdisk:用于磁盘分 ...

  7. 115 Java Interview Questions and Answers – The ULTIMATE List--reference

    In this tutorial we will discuss about different types of questions that can be used in a Java inter ...

  8. CentOS无损调整home,root磁盘的大小

    调整硬盘分区大小想增加root空间,减少home空间. 需要说明的是整个操作需要使用root用户. 1.查看硬盘使用情况. [root@Slave1 /]# df -h Filesystem Size ...

  9. Morgan Stanley telephone interview

    Today is Monday, April 28. I get a telephone call from Morgan Stanley in Shanghai. My examiner is a ...

  10. linux磁盘管理系列-LVM的使用

    LVM是什么 LVM是Linux操作系统的逻辑卷管理器. 现在有两个Linux版本的LVM,分别是 LVM1,LVM2.LVM1是一种已经被认为稳定了几年的成熟产品,LVM2 是最新最好的LVM版本. ...

随机推荐

  1. spring cloud 入门

    某种程度上 软硬件 殊途同归了 (软件模仿硬件 总线设计, 资源定位 (寻址) ) spring 是什么 EDA ( Event-driven architecture ) (SOA , SOAP , ...

  2. 微信支付成功没有回调遇到的坑 onBridgeReady getBrandWCPayRequest wx.chooseWXPay

    最近在调微信支付,遇到一个问题,就是支付成功回调不执行的. 遇到的问题就是   苹果手机 支付成功没有进到回调函数里,但是支付的时候,点击取消支付是可以进到回调函数里的.    安卓手机测试一切正常! ...

  3. JS异步上传文件

    直接调用Upload(option)方法,即可上传文件,不需要额外的插件辅助,采用原生js编写. /* *异步上传文件 *option参数 **url:上传路径 **data:上传的其他数据{id:& ...

  4. (转)Java并发编程:核心理论

    原文链接:https://www.cnblogs.com/paddix/p/5374810.html Java并发编程系列: Java 并发编程:核心理论 Java并发编程:Synchronized及 ...

  5. 如何删掉git版本库master分支的第一个commit

    这个操作会将库清空,一般来说在建库开始的时候操作. 适用场景: git init初始化版本库之后,提交第一个点之后发现这个点出问题了,但是此时内心如果有洁癖的话, 你会觉得不完美,很想把这个点干掉重来 ...

  6. Qt Creator 4.10 Beta版发布

    使用Qt Creator 4.10 Beta,现在支持固定文件,因此即使在关闭所有文件时它们仍然保持打开状态,围绕语言服务器协议支持继续集成,将Android目标添加到CMake/Qbs项目,支持Bo ...

  7. Windows 聚焦的锁屏壁纸设置为桌面壁纸

    需求: Windows的锁屏壁纸偶尔遇到非常喜欢的壁纸,想设置为桌面壁纸. 步骤如下: 1. “Windows 聚焦”的锁屏壁纸都保存在隐藏文件夹 --- Assets里. a. 打开“资源管理器 b ...

  8. SEO黑页以及门页框架和JS跳转实现方法

    在去年大家还在针对第三方博客狂轰乱炸,比如:webs.com.blogspot.com.weebly.com主要是因为本身博客平台的权重,再就是低廉的成本,主需要注册,没有域名和服务器的投入.排名也非 ...

  9. shell更改xml中的指定值

    sed -i 's;<id>.*<\/id>;<id>新内容<\/id>;g'  your.xml

  10. 【hiho1715】树的联通问题

    题目大意:给定一棵 1~n 标号的树.Tree[L,R]表示最少需要选择的边的数量使得 L~R 号点两两连通.求: \[ \sum_{L=1}^{n} \sum_{R=L}^{n} \operator ...