Is JavaScript a pass-by-reference or pass-by-value language?
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?的更多相关文章
- Does Java pass by reference or pass by value?(Java是值传递还是引用传递) - 总结
这个话题一直是Java程序员的一个热议话题,争论不断,但是不论是你百度搜也好还是去看官方的文档中所标明的也好,得到的都只有一个结论:Java只有值传递. 在这里就不贴代码细致解释了,让我们来看看一些论 ...
- 值传递:pass by value(按值传递) 和 pass by reference(引用传递)-[all]-[编程原理]
所有的编程语言,都会讨论值传递问题. 通过一个js示例直观认识 //理解按值传递(pass by value)和按引用传递(pass by reference) //pass by value var ...
- JavaScript : Array assignment creates reference not copy
JavaScript : Array assignment creates reference not copy 29 May 2015 Consider we have an array var a ...
- [GeekBand ] 利用 pass by reference -to -const 编写高效规范的 c++代码
本文参考资料 : GeekBand 侯捷老师,学习笔记 Effective C ++ 侯捷译 条款20 开发环境采用:VS2013版本 首先:分析值传递的缺点 (一) class Person{ p ...
- 学习OpenStack之 (4): Linux 磁盘、分区、挂载、逻辑卷管理 (Logical Volume Manager)
0. 背景: inux用户安装Linux操作系统时遇到的一个常见的难以决定的问题就是如何正确地评估各分区大小,以分配合适的硬盘空间.普通的磁盘分区管理方式在逻辑分区划分好之后就无法改变其大小,当一个逻 ...
- Linux 磁盘管理
Linux磁盘管理好坏管理直接关系到整个系统的性能问题. Linux磁盘管理常用三个命令为df.du和fdisk. df:列出文件系统的整体磁盘使用量 du:检查磁盘空间使用量 fdisk:用于磁盘分 ...
- 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 ...
- CentOS无损调整home,root磁盘的大小
调整硬盘分区大小想增加root空间,减少home空间. 需要说明的是整个操作需要使用root用户. 1.查看硬盘使用情况. [root@Slave1 /]# df -h Filesystem Size ...
- Morgan Stanley telephone interview
Today is Monday, April 28. I get a telephone call from Morgan Stanley in Shanghai. My examiner is a ...
- linux磁盘管理系列-LVM的使用
LVM是什么 LVM是Linux操作系统的逻辑卷管理器. 现在有两个Linux版本的LVM,分别是 LVM1,LVM2.LVM1是一种已经被认为稳定了几年的成熟产品,LVM2 是最新最好的LVM版本. ...
随机推荐
- [转载]MySQL之char、varchar和text的设计
[转载]MySQL之char.varchar和text的设计 来源:https://www.cnblogs.com/billyxp/p/3548540.html 首先我们先普及一下常识: 1.char ...
- 爬取YY评级信息
#!/usr/bin/env python # -*- coding: utf-8 -*- # @File : 爬取YY评级基本信息.py # @Author: lattesea # @Date : ...
- docker toolbox的redis 配置主从及哨兵模式保证高可用
redis 的缓存中间件安装方法,简单举例如下: 环境: docker toolbox 一 主从模式1 搜索redis镜像 docker search redis2 拉取镜像docker pul ...
- 07 Python之字典和集合
1. 什么是字典 字典是用{}表示,以key:value的形式来保存数据的,其查找效率比较高 坑: 字典存储数据时是用哈希值来存储的,算法不能变(python的) 数据必须是可哈希的(不可变的),字典 ...
- ccs之经典布局(二)(两栏,三栏布局)
接上篇ccs之经典布局(一)(水平垂直居中) 四.两列布局 单列宽度固定,另一列宽度是自适应. 1.float+overflow:auto; 固定端用float进行浮动,自适应的用overflow:a ...
- 自己实现一个简化版Mybatis框架
MyBatis框架的核心功能其实不难,无非就是动态代理和jdbc的操作,难的是写出来可扩展,高内聚,低耦合的规范的代码.本文完成的Mybatis功能比较简单,代码还有许多需要改进的地方,大家可以结合M ...
- 自动化测试报告之allure使用基础指南
差不多三个月前些的教程,然后跳槽了,自定义模块还没有写....后续也不知道有时间补上没有,最近应该会毕竟专注app测试这块了 1.github下载allure安装包:https://githu ...
- IDEA打开光标是粗黑色,backspace键、insert键点击无效的解决办法
问题描述:打开IDEA的编译器之后,界面显示的光标变粗,点击backspace键和insert键盘之后无效 解决方法:打开File——Settings——Plugins,在右侧的搜索栏中搜索IdeaV ...
- Mandelbrot集合及其渲染
什么是Mandelbrot集合? Mandelbrot集合是在复数平面上组成分形的点的集合,它正是以数学家Mandelbrot命名. Mandelbrot集合可以用复二次多项式 \[ f_c(z)=z ...
- CentOS6.5安装zabbix3.0
Server端 搭建LAMP(Linux+Apache+Mysql+PHP)环境 1.安装MySQL #安装地址:https://dev.mysql.com/downloads/repo/yum/ y ...