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


Groups > comp.lang.javascript > #27840

Re: Identity of values in Javascript

Path csiph.com!usenet.pasdenom.info!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!137.226.75.22.MISMATCH!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.javascript
Subject Re: Identity of values in Javascript
Date Sat, 23 May 2015 00:24:21 +0100
Organization A noiseless patient Spider
Lines 152
Message-ID <87d21sgpqy.fsf@bsb.me.uk> (permalink)
References <555f256b$0$12988$c3e8da3$5496439d@news.astraweb.com> <87r3q8h3yf.fsf@bsb.me.uk> <555f7d3b$0$13002$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain
Injection-Info mx02.eternal-september.org; posting-host="017616aa25f81ec581c44d76d61ba2f3"; logging-data="28264"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19DkKrS3cBPe4ggcPa8PpHko5YjXbWT+6Y="
Cancel-Lock sha1:kGUWk275u9XPX5OYJ/6AmyaQvNQ= sha1:g66mAogxliJjYZgvijbgsZVw/Jk=
X-BSB-Auth 1.8a76fc0386778ae86796.20150523002421BST.87d21sgpqy.fsf@bsb.me.uk
Xref csiph.com comp.lang.javascript:27840

Show key headers only | View raw


Steven D'Aprano <steve@pearwood.info> writes:

> On Sat, 23 May 2015 04:17 am, Ben Bacarisse wrote:
>
>> Steven D'Aprano <steve@pearwood.info> writes:
>> 
>>> Hi, I'm a newbie when it comes to Javascript, and I'm trying to
>>> understand some aspects of the language model. Please correct me if I'm
>>> wrong.
>>>
>>> Javascript has two kinds of values, simple values or primitives, and
>>> objects. There are three primitive types: number, string and boolean.
>>> Primitives are implemented as simple machine types. Everything else is an
>>> object.
>> 
>> The ECMAScript types are named with capitals, and there are a couple
>> more (Null and Undefined) but that's neither here nor there as far as
>> your point goes.  Also I'm not sure I'd say that String is implemented
>> as a simple machine type -- it's a relatively complex type compared to,
>> say, Number, -- but, again, that's not your point.
>
> True :-) but thanks for the correction anyway.
>
> As I said, I'm new to Javascript, and I keep finding sources that refer to
> (say) lower case string, and distinguish it from String. E.g.:

There's no real harm in that and Javascript is a rather vague term
anyway.  Most implementations aim to conform with the ECMAScript
specification and that uses Number etc when talking about the types.

<snip>
>>> var a = 23;  // a primitive
>>> var b = {};  // an object
>>> var c = [1, 2]  // arrays are objects too
>>>
>>> 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 C and in Pascal values don't have addresses so you can't tell if a
>> value is copied by taking the address of anything.
>
> I beg to differ. Pascal has the @ operator, which returns the address
> of the variable currently holding the value you care about. Admittedly
> it's not standard Pascal, but it is available in a number of popular
> compilers. C has the & operator.

No, you beg to agree because I agree with this and it's not at odds with
what I said!

> If you want to argue that they return
> the address of the variable, not the value, I won't disagree, but
> state that for my purposes that is fine.

Now I beg to differ.  I don't think it is enough for your purposes
because it can't tell about situations you are probably interested in
like

  f(42);
  f(g(42));

There is nothing whose address can be compared here.

>> In C, you can be certain that the object (using the C term) named by the
>> parameter has a unique address distinct from that of all of the objects
>> during its lifetime, but that's it.  You know you get a copied value
>> because of the way the semantics of function calling is defined.
>
> Yes, and you can confirm it by comparing the address of the function formal
> parameter and the address of the variable being used as argument. E.g.
> given a function foo with formal parameter a, and a variable b in an
> enclosing scope, if I call foo(b), pass by value semantics require that the
> addresses &a and &b will be different.

You still need to know the semantics of function call.  Having &a != &b
tells you something in C only because of the rest of the things you know
about C (i.e. how a call is defined to work).  I am sure you can imagine
a (nasty) language in which

  var a = 42
  define f(b) if &a != &b print("no address alias"); b = 0
  f(a)
  print(a)

prints "no address alias" and 0.  Ultimately, it's the language
definition that tells you how things get passed, and it's often very
hard to find out by poking about in a particular implementation.

>> Informally, I'd describe ECMAScript argument passing as call by sharing
>> (as in Python) because that's how it behaves.
>
> That's what I expected, but I've found so many places claiming that
> Javascript uses call by value for "simple types" and call by reference
> for "reference types" that I wanted to see for myself.
>
> (By the way, I've seen similar confusion and abuse of terminology in the
> Ruby, Python and Java camps too.)

One person's confusion is another person's explanation.  The term "call
by sharing" is rather opaque.  It does not explain much on its own, so
more usually needs to be said.

>> 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.
>> 
>>> 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.
>
> Ah, thanks again for the correction.
>
> I only need to use the var declaration once, correct?

Yes, it's a declaration.

> If I say:
>
> var x = 23;
>
> then later
>
> x = 42;
>
> the second assignment applies to the same x variable as the
> first. Correct?

Yes, provided x is still in scope.

-- 
Ben.

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


Thread

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

csiph-web