zoukankan      html  css  js  c++  java
  • 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"

    实战

  • 相关阅读:
    TensorFlow中的基本概念
    理解 tf.Variable、tf.get_variable以及范围命名方法tf.variable_scope、tf.name_scope
    深度神经网络关键词解释
    [python] os.path.join() 与 sys.path
    Git 遇到的坑
    [转] 资深程序员得到的职场经验教训
    VS CODE 快捷键
    解决VS Code使用code runner开发Python乱码问题
    熵,条件熵,互信息,交叉熵
    Visual Studio Code 支持TensorFlow配置支持
  • 原文地址:https://www.cnblogs.com/chucklu/p/11162335.html
Copyright © 2011-2022 走看看