Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #27837
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Identity of values in Javascript |
| Date | 2015-05-23 00:02 +0200 |
| Organization | PointedEars Software (PES) |
| Message-ID | <9826842.JalMral6nn@PointedEars.de> (permalink) |
| References | <555f256b$0$12988$c3e8da3$5496439d@news.astraweb.com> <87r3q8h3yf.fsf@bsb.me.uk> |
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.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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