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


Groups > comp.lang.javascript > #33535 > unrolled thread

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

Started byThomas 'PointedEars' Lahn <PointedEars@web.de>
First post2017-06-17 15:37 +0200
Last post2017-06-17 10:09 -0700
Articles 2 — 2 participants

Back to article view | Back to comp.lang.javascript

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

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

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2017-06-17 15:37 +0200
SubjectRe: console.log and the string representation of a function
Message-ID<4292181.31r3eYUQgx@PointedEars.de>
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.

[toc] | [next] | [standalone]


#33536

FromJulio Di Egidio <julio@diegidio.name>
Date2017-06-17 10:09 -0700
Message-ID<4b457bbb-66af-400e-a12f-bd38736f32e3@googlegroups.com>
In reply to#33535
On Saturday, June 17, 2017 at 3:37:36 PM UTC+2, Thomas 'PointedEars' Lahn wrote:
> Stefan Ram wrote:

@Stefan: FYI, I cannot see your posts in Google Groups, I just see the
replies if any.

> >   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”.

Had you just taken a sec to actually open that page...

> >   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.

One should still check a platform's conformance, but the rules seem pretty
clear to me:

<https://developer.mozilla.org/en-US/docs/Web/API/Console/log>
<https://console.spec.whatwg.org/#logger>

Julio

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web