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

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: console.log and the string representation of a function
Date 2017-06-17 15:37 +0200
Organization PointedEars Software (PES)
Message-ID <4292181.31r3eYUQgx@PointedEars.de> (permalink)
References <string-representation-20170617110004@ram.dialup.fu-berlin.de>

Show all headers | 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