JavaScript assign by value vs reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • howa

    JavaScript assign by value vs reference

    Hi,

    Consider a simple example, e.g.

    var a = {

    'a': 'b',
    'c': 'd'

    }

    var k = a;


    1. Is it assign by value (copying) or by reference)
    2. If it is by value/reference, how to make them assign by reference/
    value

    Howard

  • VK

    #2
    Re: JavaScript assign by value vs reference

    On Jun 1, 11:30 am, howa <howac...@gmail .comwrote:
    Hi,
    >
    Consider a simple example, e.g.
    >
    var a = {
    >
    'a': 'b',
    'c': 'd'
    >
    }
    >
    var k = a;
    >
    1. Is it assign by value (copying) or by reference)
    Why not to check by yourself?

    var a = {
    'a': 'b',
    'c': 'd'
    }

    var k = a;

    k.foo = 'bar';

    window.alert(a. foo); // 'bar'

    k gets a copy of reference held by a
    2. If it is by value/reference, how to make them assign by reference/
    value
    most libraries are having custom clone() method or equivalents. AFAIK
    it is mainly based on properties iteration and assigning to a new
    object - unless it is an instanceof Array where slice/splice trick is
    much more effective.

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: JavaScript assign by value vs reference

      howa wrote:
      Consider a simple example, e.g.
      >
      var a = {
      >
      'a': 'b',
      'c': 'd'
      >
      }
      This is initializing an Object object and assigning the reference to it to `a'.
      var k = a;
      >
      >
      1. Is it assign by value (copying) or by reference)
      As (object) references are values in ECMAScript implementations , you are
      asking the wrong question.

      You are assigning the reference value stored in `a' to `k' here. So after
      that assignment, `k' refers to the same object as `a'.
      2. If it is by value/reference, how to make them assign by reference/
      value
      You can only "copy" references to objects as you "copy" any other value; you
      cannot "copy" the object itself. However, you can "copy" certain property
      values from one object to another, or you can have one object inherit
      properties from another through the prototype chain. But that is different
      from copying the object; objects have identity.


      PointedEars
      --
      Use any version of Microsoft Frontpage to create your site.
      (This won't prevent people from viewing your source, but no one
      will want to steal it.)
      -- from <http://www.vortex-webdesign.com/help/hidesource.htm>

      Comment

      • howa

        #4
        Re: JavaScript assign by value vs reference

        On 6$B7n(B1$BF| (B, $B2<8a(B7$B; ~(B06$BJ,(B, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
        wrote:
        You can only "copy" references to objects as you "copy" any other value; you
        cannot "copy" the object itself. However, you can "copy" certain property
        values from one object to another, or you can have one object inherit
        properties from another through the prototype chain. But that is different
        from copying the object; objects have identity.
        >
        Thanks.

        So it is same as Java then.

        primitive data type are assign by value, object are assign by value
        reference.

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: JavaScript assign by value vs reference

          howa wrote:
          On 6月1日, 下午7時06分 , Thomas 'PointedEars' Lahn <PointedE...@we b.de>
          wrote:
          >You can only "copy" references to objects as you "copy" any other value; you
          >cannot "copy" the object itself. However, you can "copy" certain property
          >values from one object to another, or you can have one object inherit
          >properties from another through the prototype chain. But that is different
          >from copying the object; objects have identity.
          >
          Thanks.
          You are welcome.
          So it is same as Java then.
          Not quite. Java uses class-based, not prototype-based, inheritance that
          does not allow one object to inherit from another.
          primitive data type are assign by value, object are assign by value
          reference.
          No. Primitive values and object references are values, period.

          "Assign by reference/value" also does not strike me as being a reasonable
          term. There are "call by value" and "call by reference" but those terms
          apply neither to ECMAScript implementations nor Java.


          PointedEars
          --
          Use any version of Microsoft Frontpage to create your site.
          (This won't prevent people from viewing your source, but no one
          will want to steal it.)
          -- from <http://www.vortex-webdesign.com/help/hidesource.htm>

          Comment

          • Richard Cornford

            #6
            Re: JavaScript assign by value vs reference

            howa wrote:
            Thomas 'PointedEars' Lahn wrote:
            >You can only "copy" references to objects as you "copy" any
            >other value; you cannot "copy" the object itself. ...
            <snip>
            So it is same as Java then.
            >
            primitive data type are assign by value, object are assign
            by value reference.
            It is safe to behave as if 'primitive data types are assigned (and
            passed) by value', but they may not be. Javascript/ECMAScript has no
            operations/operators that can modify a primitive data type so it would
            be entirely possible for an implementation to internally use objects as
            its representations of primitive data types and be assigning references
            to those objects whenever code assign a primitive value to a
            variable/property. Indeed there may be good reasons for implementing
            javascript in precisely that way (for one thing it removes the need for
            the assignment process to care about the type of value it is handling).
            However, because there are no mechanisms for directly altering the value
            of a primitive data type there is no way you tell, and there are no
            consequences; it does not matter.

            Richard.

            Comment

            Working...