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


Groups > comp.lang.javascript > #27819 > unrolled thread

Identity of values in Javascript

Started bySteven D'Aprano <steve@pearwood.info>
First post2015-05-22 22:47 +1000
Last post2015-05-23 05:20 +1000
Articles 6 on this page of 26 — 8 participants

Back to article view | Back to comp.lang.javascript


Contents

  Identity of values in Javascript Steven D'Aprano <steve@pearwood.info> - 2015-05-22 22:47 +1000
    Re: Identity of values in Javascript Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-22 19:17 +0100
      Re: Identity of values in Javascript Steven D'Aprano <steve@pearwood.info> - 2015-05-23 05:02 +1000
        Re: Identity of values in Javascript Steven D'Aprano <steve@pearwood.info> - 2015-05-23 05:25 +1000
        Re: Identity of values in Javascript "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-22 23:51 +0200
        Re: Identity of values in Javascript Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-23 00:24 +0100
          Re: Identity of values in Javascript Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-23 09:35 +0200
            Re: Identity of values in Javascript Steven D'Aprano <steve@pearwood.info> - 2015-05-23 23:04 +1000
          Re: Identity of values in Javascript "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-05-23 22:27 +0200
            Re: Identity of values in Javascript Steven D'Aprano <steve@pearwood.info> - 2015-05-25 01:58 +1000
          Re: Identity of values in Javascript Steven D'Aprano <steve@pearwood.info> - 2015-05-25 02:12 +1000
            Re: Identity of values in Javascript Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-24 23:33 +0100
              Re: Identity of values in Javascript Steven D'Aprano <steve@pearwood.info> - 2015-05-25 21:47 +1000
                Re: Identity of values in Javascript Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-26 00:10 +0100
                  Re: Identity of values in Javascript Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-26 17:34 +0200
                    Re: Identity of values in Javascript Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-26 17:03 +0100
                      Re: Identity of values in Javascript Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-26 18:31 +0200
                        Re: Identity of values in Javascript Steven D'Aprano <steve@pearwood.info> - 2015-05-27 23:40 +1000
                          Re: Identity of values in Javascript Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-05-27 23:21 -0300
                            Re: Identity of values in Javascript "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2015-05-27 19:32 -0700
                            Re: Identity of values in Javascript Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-28 22:38 +0200
                    Re: Identity of values in Javascript Scott Sauyet <scott.sauyet@gmail.com> - 2015-05-26 09:06 -0700
                    Re: Identity of values in Javascript Steven D'Aprano <steve@pearwood.info> - 2015-05-27 23:02 +1000
      Re: Identity of values in Javascript Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-23 00:02 +0200
      Re: Identity of values in Javascript Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-23 09:19 +0200
    Re: Identity of values in Javascript Steven D'Aprano <steve@pearwood.info> - 2015-05-23 05:20 +1000

Page 2 of 2 — ← Prev page 1 [2]


#27912

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-05-28 22:38 +0200
Message-ID<2786260.1VnKFbDUMb@PointedEars.de>
In reply to#27907
Joao Rodrigues wrote:

> On 27/05/15 10:40, Steven D'Aprano wrote:
>> On Wed, 27 May 2015 02:31 am, Thomas 'PointedEars' Lahn wrote:
>>>    “I do not enlighten those who are not eager to learn,
>>>     nor arouse those who are not anxious
>>>     to give an explanation themselves.
>>>     If I have presented one corner of the square
>>>     and they cannot come back to me with the other three,
>>>     I should not go over the points again.”
>>
>> You cannot uniquely specify a square given only one corner -- there is no
>> way you can give the right answer except by luck. […]

You need to read between the lines.  Confucius (551–479 BCE; latinized form 
of Pidgin “Kong Fuzi”) did not teach mathematics.

> Apparently, Thomas copied and pasted a citation in the introduction of
> the 'Eloquent JavaScript':
> <http://eloquentjavascript.net/00_intro.html>

I did not.  You are the one promoting bad books and dubious Web resources, 
remember?
 
> And he seems to be confused with Confucius...

Yes, that only seems to be so.  The above saying is from Confucius as 
quoted, e.g., in “Confucius and Socrates – Teaching Wisdom” by Sanderson 
Beck:

<http://www.san.beck.org/CONFUCIUS3-How.html>

-- 
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.

[toc] | [prev] | [next] | [standalone]


#27895

FromScott Sauyet <scott.sauyet@gmail.com>
Date2015-05-26 09:06 -0700
Message-ID<988cae23-8d67-47b5-ba7e-83ec9291479e@googlegroups.com>
In reply to#27892
Thomas 'PointedEars' Lahn wrote:
> There are not going to be any concrete *correct* answers until he realizes 
> that there is no "Javascript".  With *all* the implications of that fact.

All the implications of a single fact tend to spread pretty far and wide.
Perhaps William Blake was speaking of such with "To see the world in a 
grain of sand".  In fact, I can imagine one might devote an entire life to
following up the ripples spread from just one tiny piece of
(mis?)-information.  In fact, perhaps someone has:

<http://i.imgur.com/5UhdL.png>

  -- Scott

[toc] | [prev] | [next] | [standalone]


#27905

FromSteven D'Aprano <steve@pearwood.info>
Date2015-05-27 23:02 +1000
Message-ID<5565c050$0$13012$c3e8da3$5496439d@news.astraweb.com>
In reply to#27892
On Wed, 27 May 2015 01:34 am, Thomas 'PointedEars' Lahn wrote:

> there is no “Javascript”.

And here I thought this was comp.lang.javascript.

If you would like to discuss implementation-specific differences between
different Javascript engines and implementations (say, SpiderMonkey, Rhino,
V8, Tamarin, Chakra, etc), and how they relate to different versions of the
ECMAScript standard, that would be a useful answer.

Pretending that there is "no" Javascript is not a useful answer.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#27837

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-05-23 00:02 +0200
Message-ID<9826842.JalMral6nn@PointedEars.de>
In reply to#27826
Ben Bacarisse wrote:

> Informally, I'd describe ECMAScript argument passing as call by sharing
> (as in Python) because that's how it behaves.  The full specification is
> in ECMA 262 edition 5.1 (there's a new version, 6, on the way).  People
> more expert than I will step in to say where (if anywhere) the corner
> cases are that make this informal description untrue.

I think it is less error-prone, easier to understand, and I recommend to use 
the concept “call-by-value” throughout when explaining ECMAScript and its 
implementations.

Then you are always passing values as arguments to functions.  This is easy 
to understand for primitive values (except when you come from languages like 
Python where [AFAIK] there are no primitive values).  As for objects, you 
are _not_ passing an object to a function, you are passing a *reference to* 
an object, and there can be several references to the same object. ( By 
“object” I really mean an object as in Object-Oriented Programming, as 
compared to primitive values which are neither objects nor references to 
objects in that sense in ECMAScript¹.)  This explains the “sharing”: as the 
references resolve to the same object, you can modify the object from 
different places in the code.

Consider the following program:

  function f (obj)
  {
    obj.foo = 23;
    obj.bar = "baz";
  }

  var o = {};
  o.foo = 42;
  f(o);
  console.log(o.bar);

This is what happens²:
                               ,----------------.
  var o = {};          o ----> | object: Object |
                               `----------------'

                               ,-------------------.
  o.foo = 42;          o ----> | object: Object    |
                               |-------------------|
                               | +foo: Number = 42 |
                               `-------------------'

  f(o);                         ,------------------.
                       o ----> | object: Object    |
                               |-------------------|
                               | +foo: Number = 42 |
                               `-------------------'

                                ,------------------.
    obj.foo = 23;      o ----> | object: Object    | <---- obj
                               |-------------------|
                               | +foo: Number = 23 |
                               `-------------------'

                               ,----------------------.
    obj.bar = "baz";   o ----> | object: Object       | <---- obj
                               |----------------------|
                               | +foo: Number = 23    |
                               | +bar: String = "baz" |
                               `----------------------'

                               ,----------------------.
  console.log(o.bar);  o ----> | object: Object       |
                               |----------------------|
                               | +foo: Number = 23    |
                               | +bar: String = "baz" |
                               `----------------------'


See also [2] which I helped to write.

_______
¹  This can be difficult to see because primitive values are converted, if
   possible, to objects of a corresponding object type when a syntax element 
   referring to a value (referring as in the *Specification’s* Reference 
   type) is used in property access syntax (e.g. “Math.PI.toString(2)” is 
   equivalent to “new Number(Math.PI).toString(2)”).  Years ago in 
   <news:de.comp.lang.javascript>, Georg Maaß [1] introduced the term 
   „Hüll-Objekt“ (approx. “encapsulating object”) for this mechanism.
²  This Object instance inherits from Object.prototype, which is not
   depicted here.

[1] <http://georg-maass.de/>
[2] <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions>
-- 
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.

[toc] | [prev] | [next] | [standalone]


#27842

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-05-23 09:19 +0200
Message-ID<1640362.adkkAqJXqZ@PointedEars.de>
In reply to#27826
Ben Bacarisse wrote:

> Steven D'Aprano <steve@pearwood.info> writes:
>> In Javascript, I could use something
>> like this:
>>
>>
>> function test(arg) {
>>     if (addr(arg) != addr(x)) {
>>         return "copied"
>>     }
>>     else {
>>         return "not copied"
>>     }
>> }
>>
>> x = 23;
> 
> This is a little odd in ECMAScript.  Put a 'var' in front to make a new
> named variable.  Otherwise you are adding (or modifying) the 'x'
> property to the global object.

Not necessarily.

-- 
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.

[toc] | [prev] | [next] | [standalone]


#27831

FromSteven D'Aprano <steve@pearwood.info>
Date2015-05-23 05:20 +1000
Message-ID<555f816e$0$13010$c3e8da3$5496439d@news.astraweb.com>
In reply to#27819
On Sat, 23 May 2015 03:14 am, Stefan Ram wrote:

> Supersedes: <pass-20150522184523@ram.dialup.fu-berlin.de>
> 
> Steven D'Aprano <steve@pearwood.info> writes:
>>Javascript has two kinds of values, simple values or primitives, and
>>objects. There are three primitive types: number, string and boolean.
> 
>   string
>   number
>   boolean
>   null
>   undefined
>   symbol (new in ECMAScript 6)

Thanks for the correction.


>>I understand that for primitives, if I pass a value to a function,
>>Javascript uses pass by value: the value is copied. (This is the same
>>behaviour as, say, C or Pascal.) While accepting that on faith is fine,
>>I'd like to actually demonstrate it with some Javascript code.
>>In C or Pascal, I could use the "address of" operator to compare the value
>>inside the function with the value outside the function, to see if they
>>referred to the same memory location. In Javascript, I could use something
>>like this:
> 
>   A value does not have an address.

Numbers in Javascript are IEEE 754 64-bit floating point quantities, so if I
do "var x = 23;" the 64-bit float with value 23.0 must be somewhere.
Perhaps you mean that the address is unavailable to Javascript code?



>   Expressions for such 
>   values are known as »prvalues« in C++. With (pr)values,
>   it makes no sense to make this distinction between »pass by
>   value" and "pass by reference". 

I know very little about C++, but speaking more generally, pass by value and
pass by reference have VERY different semantics and it is nonsense to say
that there is no distinction between them. Pass by reference has strict
requirements: the argument passed must be an actual variable, not an
arbitrary expression or constant (early Fortran compilers were notorious
for a bad bug where you could literally change the value of numeric
constants by passing then by reference), and the function parameter is
treated as an alias to that variable for assignment. Pass by value is very
different.


>   One might also say it this 
>   way: "The value of a prvalue doubles as it's reference."

When I assign a value to a variable:

var x = 42;

the value of x that I am talking about is 42, not some reference to 42.

 
>>                                    Is there anything I can write in
>>Javascript to demonstrate that when I pass a primitive value into a
>>function, it is copied, but when I pass an object into the function, it is
>>not?
> 
>   In the case of a value, you show it by showing that it just works.

What does "it just works" mean? What's "it"? That I can pass an argument to
a function?

Unfortunately, "I can pass arguments to functions" works equally well
whether the calling convention is by value, by name, by reference, by need,
by sharing, or something else. The only calling convention that can be
ruled out is call by reference, because I can pass expressions as arguments
(pass by reference parameters must be lvalues).

 
>   You can show that objects are not copied in this case
>   by showing alias effects.
> 
> function modify( object ){ object.field = 5; }
> function main()
> { var object = {}; modify( object ); console.log( object.field ); }
> main();


Yes, I'm aware of that. But I'm asking specifically about primitive types,
not objects. Using rhino:


js> function modify( object ){
  > object.field = 5;
  > print(object.field);
  > }
js> x = 23;
23
js> modify(x);
undefined


So your test doesn't work here.



-- 
Steven

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.javascript


csiph-web