Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Identity of values in Javascript Date: Sat, 23 May 2015 00:02:25 +0200 Organization: PointedEars Software (PES) Lines: 94 Message-ID: <9826842.JalMral6nn@PointedEars.de> References: <555f256b$0$12988$c3e8da3$5496439d@news.astraweb.com> <87r3q8h3yf.fsf@bsb.me.uk> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1432332320 15794 eJwFwYERACEIA7CV5GwLjtNH2X+ET7gV6oQocDjNV4cK4GZ6PnMvVNtmtd7UurOBvHPCCv8gxxFp (22 May 2015 22:05:20 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Fri, 22 May 2015 22:05:20 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwFwQkBwDAIA0BL5QtFTmHBv4TdhUEw6Qh4bOx3UXkGaZccs/mCqrGvqyUhGeUSpJQW8RR92xtwclf2/Ep4FX0= Cancel-Lock: sha1:AGoIOs01dLfzo5BYZQNGVz/ZPoU= X-NNTP-Posting-Host: eJwFwQkBwCAMA0BLMJqQyqGffwm7w+FmXiNoGIzavFQOpTy4hI/OnaeDfLgWokU2pvr1rB8ScRFY Xref: csiph.com comp.lang.javascript:27837 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 , 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] [2] -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.