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


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

Re: Can somebody explain this code

Started bytony@mountifield.org (Tony Mountifield)
First post2016-03-23 23:46 +0000
Last post2016-03-24 11:22 +0000
Articles 6 — 5 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: Can somebody explain this code tony@mountifield.org (Tony Mountifield) - 2016-03-23 23:46 +0000
    Re: Can somebody explain this code Aleksandro <aleksandro@gmx.com> - 2016-03-23 23:58 -0300
    Re: Can somebody explain this code Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-24 04:20 +0100
      Re: Can somebody explain this code "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-24 11:23 +0100
        Re: Can somebody explain this code Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-24 20:32 +0100
      Re: Can somebody explain this code John Harris <niam@jghnorth.org.uk.invalid> - 2016-03-24 11:22 +0000

#30090 — Re: Can somebody explain this code

Fromtony@mountifield.org (Tony Mountifield)
Date2016-03-23 23:46 +0000
SubjectRe: Can somebody explain this code
Message-ID<ncv9sp$9b2$1@softins.softins.co.uk>
In article <ncv2un$mlp$1@dont-email.me>,
Tony Johansson <johansson.andersson@telia.com> wrote:
> I know that functions behave much like variables such as
> var myFunc = function() { alert("This is my anonymous function");}
> 
> But in the code below when I have (a && b)(); only function b is called and
> when I have  (a || b)();  only function a is called
> 
> If a = null in this example (a || b)();   then b is called.
> 
> Can somebody explain what is happening here when I have this kind of strange 
> looking code.
> I do this just because of some testing how it works.
> 
> If both a and b is null I get error because then the expression does not 
> produce a function value which is correct.
> 
> <script type="text/javascript">
>         function a()
>         {
>             alert("function a");
>         };
> 
>         function b()
>         {
>             alert("function b");
>         };
> 
>        (a && b)(); //here only b is called

The expression (a && b) returns a if a is not truthy, but returns b if a is truthy.
In this case a and b are pointers to functions, which are always truthy. So (a && b)
has the value b, which is then called by virtue of the following ().

>         (a || b)(); // here only a is called

The expression (a || b) returns a if a is truthy, otherwise returns b. So (a || b)
always has the value a when a is a function pointer, and this gets called by the ().

>     </script>
> 
> //Tony 

Cheers
Tony (another one!)
-- 
Tony Mountifield
Work: tony@softins.co.uk - http://www.softins.co.uk
Play: tony@mountifield.org - http://tony.mountifield.org

[toc] | [next] | [standalone]


#30094

FromAleksandro <aleksandro@gmx.com>
Date2016-03-23 23:58 -0300
Message-ID<ncvkud$ba8$1@dont-email.me>
In reply to#30090
On 23/03/16 20:46, Tony Mountifield wrote:
> In article <ncv2un$mlp$1@dont-email.me>,
> Tony Johansson <johansson.andersson@telia.com> wrote:
>> I know that functions behave much like variables such as
>> var myFunc = function() { alert("This is my anonymous function");}
>>
>> But in the code below when I have (a && b)(); only function b is called and
>> when I have  (a || b)();  only function a is called
>>
>> If a = null in this example (a || b)();   then b is called.
>>
>> Can somebody explain what is happening here when I have this kind of strange 
>> looking code.
>> I do this just because of some testing how it works.
>>
>> If both a and b is null I get error because then the expression does not 
>> produce a function value which is correct.
>>
>> <script type="text/javascript">
>>         function a()
>>         {
>>             alert("function a");
>>         };
>>
>>         function b()
>>         {
>>             alert("function b");
>>         };
>>
>>        (a && b)(); //here only b is called
> 
> The expression (a && b) returns a if a is not truthy, but returns b if a is truthy.
> In this case a and b are pointers to functions, which are always truthy. So (a && b)
> has the value b, which is then called by virtue of the following ().
> 
>>         (a || b)(); // here only a is called
> 
> The expression (a || b) returns a if a is truthy, otherwise returns b. So (a || b)
> always has the value a when a is a function pointer, and this gets called by the ().
> 
>>     </script>
>>
>> //Tony 
> 
> Cheers
> Tony (another one!)

Truthy, eh?  lol

[toc] | [prev] | [next] | [standalone]


#30096

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-03-24 04:20 +0100
Message-ID<3102164.rQk1h68GbR@PointedEars.de>
In reply to#30090
Stefan Ram wrote:

> tony@mountifield.org (Tony Mountifield) writes:
>>The expression (a && b) returns a if a is not truthy, but returns b if a
>>is truthy. In this case a and b are pointers to functions, which are
>>always truthy. So (a && b) has the value b, which is then called by virtue
>>of the following ().
> 
>   I have just searched the ECMAScript Standard for uses of the
>   word »pointer«. Found none. That makes me think about the
>   meaning of the above expression »pointers to functions«.

There are no pointers to functions.  Proper terminology includes: “reference 
to a Function instance”, “reference to a function object”, and “reference to 
a function”.  Also, “truthy” is ambiguous [1], and “falsy” is not a proper 
English word in this sense [2]; I prefer to use “true-value” and “false-
value” instead to mean values that, if passed to the ToBoolean algorithm, 
would result in “true” and “false”, respectively.

BTW, in English it is unusual to use guillemets for quotation marks. [3]

Likewise, when discussing ECMAScript one would indent the source code 
instead of the prose.  You must have realized by now that you are the only 
one in Usenet doing the latter, making discussions in which you participated 
*harder* to read.

[1] 
<http://www.oxforddictionaries.com/definition/english/truthy?searchDictCode=all>
[2] 
<http://www.oxforddictionaries.com/definition/english/falsies?q=falsy&searchDictCode=all>
[3] <https://en.wikipedia.org/wiki/Quotation_marks_in_English>

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

[toc] | [prev] | [next] | [standalone]


#30102

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2016-03-24 11:23 +0100
Message-ID<XnsA5D573D27946Aeejj99@194.109.6.166>
In reply to#30096
Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote on 24 Mar 2016 in 
comp.lang.javascript:

> There are no pointers to functions.  Proper terminology includes:

Well Thomas, absense of proper terminology does not precludes existence.

[The same goes for "Javascript", btw]

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#30111

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-03-24 20:32 +0100
Message-ID<2654858.XfECs9sPCU@PointedEars.de>
In reply to#30102
Evertjan. wrote:

> Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote on 24 Mar 2016 in
> comp.lang.javascript:
>> There are no pointers to functions.  Proper terminology includes:
> 
> Well Thomas, absense of proper terminology does not precludes existence.
               ^^^^^^^                       ^^^^^^^^^^^^^^^^^^
Those who do not know proper English should not presume to lecture others 
about it.
 
> [The same goes for "Javascript", btw]

Nonsense.

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

[toc] | [prev] | [next] | [standalone]


#30104

FromJohn Harris <niam@jghnorth.org.uk.invalid>
Date2016-03-24 11:22 +0000
Message-ID<8dj7fblpkjpt9m5qckoojbkmva8fqrhfmo@4ax.com>
In reply to#30096
On Thu, 24 Mar 2016 04:20:45 +0100, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:

  <snip>
>There are no pointers to functions.  Proper terminology includes: “reference 
>to a Function instance”, “reference to a function object”, and “reference to 
>a function”.
  <snip>

What nonsense!

When ECMA 262 Ed 6 uses the word "reference", all lower case, it is
using the word to mean something referring to something : to a
Standard, to some part of ECMA 262, to some part of a program's source
code, or to a property. It is a general word, not a defined piece of
ECMAScript terminology.

When a value is specified to be an object or a primitive string an
implementation of ECMAScript will almost certainly use a pointer to
what might be a large data structure. It can do this provided the
Standard is still obeyed. Calling it a pointer is perfectly correct. 

The word reference can be used when talking about the meaning of
source code that uses a value implemented internally as a
pointer-to-data. The word indicates that the language does not allow
you to know about or manipulate pointers themselves. Execution
automatically follows a pointer to its destination data. The word is
still not an ECMAScript-defined term.

  John

PS Annex E of ECMA 262 Ed 6 says :
  6.2.3: In ECMAScript 2015, Function calls are not allowed to return
  a Reference value.

[toc] | [prev] | [standalone]


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


csiph-web