Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #29201

Re: array-like objects

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: array-like objects
Date 2016-01-10 09:03 +0100
Organization PointedEars Software (PES)
Message-ID <4043891.iRTcHlnhip@PointedEars.de> (permalink)
References (8 earlier) <2389107.ZQXF91zPQe@PointedEars.de> <9rqv8blasgs3525d1km3qd0ikpm0ng3a49@4ax.com> <value-20160109154923@ram.dialup.fu-berlin.de> <ntp29b9o9jdu7paoe22f9q3q4l1sibe5jb@4ax.com> <reference-20160109213450@ram.dialup.fu-berlin.de>

Show all headers | View raw


Stefan Ram wrote:

> John Harris <niam@jghnorth.org.uk.invalid> writes:
>> But ECMAScript objects belong to neither of these cases. If you do
>> a = b when a holds an object then changing the inside of the object
>> held by a also changes the inside of the object held by b even when a
>> and b are in different places. This is what's peculiar, or, at least,
>> what's different from numbers, strings, etc.
> 
> |< a = Math
> 
>   A variable can have a value, and the value of the
>   variable »a« now is the object that also is known
>   as »Math«.

No, the value of “a” is then a reference to the same object that is referred 
to by “Math”.  *Objects have identity, not name.*

Before:

         {[built-in properties]} <--- Math
 
After:

  a ---> {[built-in properties]} <--- Math

“Math” is actually the name of a built-in property of the global object 
whose value is a reference to “the Math object”:

                                      ,----------------------------.
                                      : global object              :
                                      :----------------------------:
  a ---> {[built-in properties]} <----:-+ Math                     :
                                      : [more built-in properties] :
                                      `----------------------------'

<http://www.ecma-international.org/ecma-262/6.0/#sec-other-properties-of-the-global-object>
<http://www.ecma-international.org/ecma-262/6.0/#sec-math>
<http://www.ecma-international.org/ecma-262/6.0/#sec-math-object>

> |< b = Math
>  
>   Now the value of »b« also is the object also known as »Math«.

No, now the value of “b” is a reference to the same object that is already 
referred to by the values of “Math” and “a”:

  a ---> {[built-in properties]} <--- Math
                    ^
                    :
                    :
                    b

It really is very simple: *Object references are values.*  When an 
identifier or property access is evaluated, and the result of the evaluation 
is an object reference, and it is on the right-hand side of a simple 
assignment, then that object reference is copied and made the value of 
whatever is on the left-hand side of the assignment.  

So *there is no algorithmic difference between primitive values and object 
references with regard to simple assignment*.

<http://www.ecma-international.org/ecma-262/6.0/#sec-assignment-operators-runtime-semantics-evaluation>

>   This is possible, two different variables /can/ contain the same value.

True, but the value thus copied is _not_ the object, but the object 
*reference*.  It would be very inefficient if the entire data of an object 
would be copied on simple assignment, so that does not happen.  Instead, on 
high level, the object reference is copied, and on low level the address of 
the location in the heap memory where the object data is stored, would be 
copied.
 
> |< a.c = 27
> 
>   I set the property »c« of the value of »a« to 27,
>   thus, I set the property »c« of this object »Math«
>   to 27.

No, you set the property “c” of an object that by now is referred to by the 
values of “a”, “b“, and “Math”.

Before:

  a ---> {[built-in properties]       } <--- Math
                      ^
                      :
                      :
                      b

After:
 
  a ---> {[built-in properties], c: 27} <--- Math
                      ^
                      :
                      :
                      b

Now, in theory, you could perform a “delete” operation in succession on “a”, 
“b”, and “Math”, so that they would no longer refer to said object:

  1.     {[built-in properties], c: 27} <--- Math
                      ^
                      :
                      :
                      b

  2.     {[built-in properties], c: 27} <--- Math

  3.     {[built-in properties], c: 27}

Now the object exists in memory, but nothing refers to it.  So it can be 
marked for garbage collection, and eventually the memory reserved for it
can be freed.

> |< b.c
> |> 27
> 
>   I read the property »c« of the value of »b«, thus,
>   of this object Math.

But that would not be the case if the object were the value copied; so this 
idea is disproved.
 
-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: array-like objects Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2016-01-02 13:27 -0200
  Re: array-like objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-02 20:48 +0100
    Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-03 15:50 +0000
      Re: array-like objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-03 16:59 +0100
        Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-04 11:15 +0000
          Re: array-like objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-04 15:27 +0100
            Re: array-like objects Tim Streater <timstreater@greenbee.net> - 2016-01-04 15:57 +0000
            Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-05 16:47 +0000
              Re: array-like objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-05 20:13 +0100
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-06 16:24 +0000
                Re: array-like objects Aleksandro <aleksandro@gmx.com> - 2016-01-06 14:45 -0300
                Re: array-like objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-06 19:27 +0100
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-07 11:00 +0000
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-08 17:02 +0000
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-09 20:07 +0000
                Re: array-like objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-10 09:03 +0100
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-10 10:03 +0000
                Re: array-like objects Andrew Poulos <ap_prog@hotmail.com> - 2016-01-10 23:21 +1100
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-10 15:59 +0000
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-10 15:30 +0000
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-11 11:03 +0000
                Re: array-like objects "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-01-11 10:36 -0800
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-12 10:59 +0000
                Re: array-like objects "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-01-12 12:47 +0100
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-06 10:50 +0000
                Re: array-like objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-09 05:01 +0100
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-09 19:07 +0000
                Re: array-like objects John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-09 19:23 +0000
  Re: array-like objects Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2016-01-03 04:26 -0800
    Re: array-like objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-03 16:54 +0100
      Re: array-like objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-04 00:18 +0100

csiph-web