[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Re: Can somebody explain this code

tony

3/23/2016 11:47:00 PM

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.sof...
Play: tony@mountifield.org - http://tony.mount...
6 Answers

ram

3/24/2016 12:40:00 AM

0

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

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

Aleksandro

3/24/2016 2:58:00 AM

0

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

Thomas 'PointedEars' Lahn

3/24/2016 3:21:00 AM

0

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?searchDictCo...
[2]
<http://www.oxforddictionaries.com/definition/english/falsies?q=falsy&searchDictCo...
[3] <https://en.wikipedia.org/wiki/Quotation_marks_in_E...

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Evertjan.

3/24/2016 10:23:00 AM

0

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)

John Harris

3/24/2016 11:22:00 AM

0

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.

Thomas 'PointedEars' Lahn

3/24/2016 7:33:00 PM

0

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.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.