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


Groups > comp.lang.javascript > #33535

Re: console.log and the string representation of a function

Path csiph.com!aioe.org!news.mb-net.net!open-news-network.org!.POSTED!not-for-mail
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: console.log and the string representation of a function
Date Sat, 17 Jun 2017 15:37:30 +0200
Organization PointedEars Software (PES)
Lines 88
Message-ID <4292181.31r3eYUQgx@PointedEars.de> (permalink)
References <string-representation-20170617110004@ram.dialup.fu-berlin.de>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
NNTP-Posting-Host 11.237.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Trace gwaiyur.mb-net.net 1497706651 3354 178.197.237.11 (17 Jun 2017 13:37:31 GMT)
X-Complaints-To abuse@open-news-network.org
NNTP-Posting-Date Sat, 17 Jun 2017 13:37:31 +0000 (UTC)
User-Agent KNode/4.14.2
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Xref csiph.com comp.lang.javascript:33535

Show key headers only | View raw


Stefan Ram wrote:

>   MDN docs/Web/API/Console/log says:
> 
>       »obj1 ... objNA
> 
>       list of JavaScript objects to output.

That is wrong to begin with.  The arguments do not have to be (references 
to) “JavaScript objects”.
  
>   Now look at my console transcript obtained using a recent Firefox:
                                                       ^^^^^^^^^^^^^^
Will you ever learn to properly describe your test environment?
 
> |0>  x = ()=>0;
> |1<  function x()
> 
> |2>  x + ""
> |3<  "()=>0"
> 
> |4>  x.toString = ()=>"Alpha";
> |5<  function x.toString()
> 
> |6>  x + ""
> |7<  "Alpha"
> 
> |8>  console.log( x )
> |9<  function x()
> 
>   Line 7 shows what I'd call »string representation«.
>   But console.log shows something else (line 9).

Obviously it is a matter of definition.

>   (Compare this behavior with the MDN quotation from above.)

“console” is not specified in any Edition of ECMAScript; so console.log() is 
a *host* object’s method.  And MDN is a *wiki*.

>   Is the output of lines 7 and 9 what you would have expected?

Yes, it makes more sense to output the real source of the function if the 
value is used standalone.

>   ECMAScript has a section »19.2.3.5 Function.prototype.
>   toString()«, which might express that the behavior is implementation-
>   dependent anyway?

Which *Edition* of ECMAScript are you referring to?  That is important.
(Will you ever learn to cite properly?)

Since ECMAScript 2015, the return value of the built-in .toString() method 
of "normal", user-defined function instances must compile to an equivalent 
function instance:

<http://www.ecma-international.org/ecma-262/6.0/#sec-function.prototype.tostring>

| toString Representation Requirements:
|
| […]
| · If the object was defined using ECMAScript code and the returned string
|   representation is not in the form of a MethodDefinition or 
|   GeneratorMethod then the representation must be such that if the string 
|   is evaluated, using `eval` in a lexical context that is equivalent to
|   the lexical context used to create the original object, it will result
|   in a new functionally equivalent object. In that case the returned
|   source code must not mention freely any variables that were not
|   mentioned freely by the original function’s source code, even if these
|   “extra” names were originally in scope.
| · […]

IOW, this enables function serialization/deserialization, e.g. source code 
reflection, and storing functions as and retrieving them from JSON data.  
However, previous Editions, which were implemented, did not make that 
condition; therefore, function serialization/deserialization still is 
unreliable unless you know the runtime environment for certain.

>   It does not seem to mention explicitly the possibility that the toString
>   function of a function was defined by a user.

It does not have to.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

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


Thread

Re: console.log and the string representation of a function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-06-17 15:37 +0200
  Re: console.log and the string representation of a function Julio Di Egidio <julio@diegidio.name> - 2017-06-17 10:09 -0700

csiph-web