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


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

Can somebody explain this code

Started by"Tony Johansson" <johansson.andersson@telia.com>
First post2016-03-23 22:51 +0100
Last post2016-05-07 13:02 -0300
Articles 10 — 4 participants

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


Contents

  Can somebody explain this code "Tony Johansson" <johansson.andersson@telia.com> - 2016-03-23 22:51 +0100
    Re: Can somebody explain this code Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-24 20:30 +0100
      Re: Can somebody explain this code Aleksandro <aleksandro@gmx.com> - 2016-03-24 20:19 -0300
        Re: Can somebody explain this code Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-27 22:44 +0200
          Re: Can somebody explain this code Aleksandro <aleksandro@gmx.com> - 2016-03-27 18:22 -0300
            Re: Can somebody explain this code Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-28 01:07 +0200
              Re: Can somebody explain this code Aleksandro <aleksandro@gmx.com> - 2016-03-27 20:26 -0300
                Re: Can somebody explain this code Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-05-04 05:51 +0200
                  Re: Can somebody explain this code "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-05-04 08:55 +0200
                  Re: Can somebody explain this code Aleksandro <aleksandro@gmx.com> - 2016-05-07 13:02 -0300

#30093 — Can somebody explain this code

From"Tony Johansson" <johansson.andersson@telia.com>
Date2016-03-23 22:51 +0100
SubjectCan somebody explain this code
Message-ID<ncv2un$mlp$1@dont-email.me>
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

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

    </script>

//Tony 

[toc] | [next] | [standalone]


#30110

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-03-24 20:30 +0100
Message-ID<2380128.LjnWGdRavf@PointedEars.de>
In reply to#30093
Tony Johansson wrote:

> I know that functions behave much like variables such as
> var myFunc = function() { alert("This is my anonymous function");}

A misconception.  Instead, the variable “myFunc” is being declared, and 
initialized with (i.e., being assigned as first value) a reference to an 
object, a Function instance.  That object’s “name” property has the value "" 
(as inherited from the Function prototype object); it is an anonymous 
function as it was created with what is commonly called an “anonymous 
function expression” (AFE).  [The right-hand side is equivalent to a 
“lambda” expression, e.g. in Python.]

It is *functionally* equivalent to

  var myFunc;
  myFunc = function () { alert("This is my anonymous function"); };

(In that case, “myFunc” is assigned the “undefined” value first.)
 
<http://www.ecma-international.org/ecma-262/6.0/#sec-variable-statement>
<http://www.ecma-international.org/ecma-262/6.0/#sec-function-instances-name>

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


#30112

FromAleksandro <aleksandro@gmx.com>
Date2016-03-24 20:19 -0300
Message-ID<nd1sgj$esr$1@dont-email.me>
In reply to#30110
On 24/03/16 16:30, Thomas 'PointedEars' Lahn wrote:
> Tony Johansson wrote:
> 
>> I know that functions behave much like variables such as
>> var myFunc = function() { alert("This is my anonymous function");}
> 
> A misconception.  Instead, the variable “myFunc” is being declared, and 
> initialized with (i.e., being assigned as first value) a reference to an 
> object, a Function instance.  That object’s “name” property has the value "" 
> (as inherited from the Function prototype object); it is an anonymous 
> function as it was created with what is commonly called an “anonymous 
> function expression” (AFE).  [The right-hand side is equivalent to a 
> “lambda” expression, e.g. in Python.]
> 
> It is *functionally* equivalent to
> 
>   var myFunc;
>   myFunc = function () { alert("This is my anonymous function"); };
> 
> (In that case, “myFunc” is assigned the “undefined” value first.)

Would the VM actually assign undefined first? I doubt it.

> <http://www.ecma-international.org/ecma-262/6.0/#sec-variable-statement>
> <http://www.ecma-international.org/ecma-262/6.0/#sec-function-instances-name>

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


#30119

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-03-27 22:44 +0200
Message-ID<3457976.2eFHA4iNFV@PointedEars.de>
In reply to#30112
Aleksandro wrote:

> On 24/03/16 16:30, Thomas 'PointedEars' Lahn wrote:
>>   var myFunc;
>>   myFunc = function () { alert("This is my anonymous function"); };
>> 
>> (In that case, “myFunc” is assigned the “undefined” value first.)
> 
> Would the VM actually assign undefined first? I doubt it.

On what grounds?
 
>> <http://www.ecma-international.org/ecma-262/6.0/#sec-variable-statement>

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


#30120

FromAleksandro <aleksandro@gmx.com>
Date2016-03-27 18:22 -0300
Message-ID<nd9ipa$7r2$1@dont-email.me>
In reply to#30119
On 27/03/16 17:44, Thomas 'PointedEars' Lahn wrote:
> Aleksandro wrote:
> 
>> On 24/03/16 16:30, Thomas 'PointedEars' Lahn wrote:
>>>   var myFunc;
>>>   myFunc = function () { alert("This is my anonymous function"); };
>>>
>>> (In that case, “myFunc” is assigned the “undefined” value first.)
>>
>> Would the VM actually assign undefined first? I doubt it.
> 
> On what grounds?

Well, there's a thing called optimization... you know...

PD: what happened to the plonk BTW? <241983454.ARWANPjLTT@PointedEars.de>

>>> <http://www.ecma-international.org/ecma-262/6.0/#sec-variable-statement>

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


#30122

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-03-28 01:07 +0200
Message-ID<2497856.AOaF6Xk2y9@PointedEars.de>
In reply to#30120
Aleksandro wrote:

> On 27/03/16 17:44, Thomas 'PointedEars' Lahn wrote:
>> Aleksandro wrote:
>>> On 24/03/16 16:30, Thomas 'PointedEars' Lahn wrote:
>>>>   var myFunc;
>>>>   myFunc = function () { alert("This is my anonymous function"); };
>>>>
>>>> (In that case, “myFunc” is assigned the “undefined” value first.)
>>> Would the VM actually assign undefined first? I doubt it.
>> On what grounds?
> 
> Well, there's a thing called optimization... you know...

Although successive lines, they are not necessarily processed so.

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


#30123

FromAleksandro <aleksandro@gmx.com>
Date2016-03-27 20:26 -0300
Message-ID<nd9q0f$2h4$1@dont-email.me>
In reply to#30122
On 27/03/16 20:07, Thomas 'PointedEars' Lahn wrote:
> Aleksandro wrote:
> 
>> On 27/03/16 17:44, Thomas 'PointedEars' Lahn wrote:
>>> Aleksandro wrote:
>>>> On 24/03/16 16:30, Thomas 'PointedEars' Lahn wrote:
>>>>>   var myFunc;
>>>>>   myFunc = function () { alert("This is my anonymous function"); };
>>>>>
>>>>> (In that case, “myFunc” is assigned the “undefined” value first.)
>>>> Would the VM actually assign undefined first? I doubt it.
>>> On what grounds?
>>
>> Well, there's a thing called optimization... you know...
> 
> Although successive lines, they are not necessarily processed so.

But they might.

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


#30388

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-05-04 05:51 +0200
Message-ID<3332181.Nmlc57RfOh@PointedEars.de>
In reply to#30123
Aleksandro wrote:

> On 27/03/16 20:07, Thomas 'PointedEars' Lahn wrote:
>> Aleksandro wrote:
>>> On 27/03/16 17:44, Thomas 'PointedEars' Lahn wrote:
>>>> Aleksandro wrote:
>>>>> On 24/03/16 16:30, Thomas 'PointedEars' Lahn wrote:
>>>>>>   var myFunc;
>>>>>>   myFunc = function () { alert("This is my anonymous function"); };
>>>>>>
>>>>>> (In that case, “myFunc” is assigned the “undefined” value first.)
>>>>> Would the VM actually assign undefined first? I doubt it.
>>>> On what grounds?
>>> Well, there's a thing called optimization... you know...
>> Although successive lines, they are not necessarily processed so.
> 
> But they might.

I am not convinced.

There are at least four points that you are overlooking here:

1. It was an *example* demonstrating functional equivalence of lines of
   source code.

2. myFunc = function () { alert("This is my anonymous function"); };
   var myFunc;
   
   is equivalent to the previous code, too.

3. There can be source code between declaration and assignment, and in the 
   assignment, referring to the variable.  The value of the variable has to
   be the “undefined” value then.

4. There is not only one virtual machine.  It need not even *be* a *virtual*
   machine; for example, Google V8 JIT-compiles source code directly into 
   machine code.

   <https://developers.google.com/v8/design>

Taking unnecessary risks based on wild guesses and wishful thinking is the 
mark of a wannabe.

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


#30389

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2016-05-04 08:55 +0200
Message-ID<XnsA5FE5AB9A2A54eejj99@194.109.6.166>
In reply to#30388
Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote on 04 May 2016 in 
comp.lang.javascript:

> Taking unnecessary risks based on wild guesses and wishful thinking is the 
> mark of a wannabe.

You must be so lonely, marking Thom, running around among us wannabees,
abhorring all joyfull risks and wishfull guesses that make our lives so 
bearable.

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

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


#30407

FromAleksandro <aleksandro@gmx.com>
Date2016-05-07 13:02 -0300
Message-ID<ngl3cg$r3d$1@dont-email.me>
In reply to#30388
On 04/05/16 00:51, Thomas 'PointedEars' Lahn wrote:
> Aleksandro wrote:
> 
>> On 27/03/16 20:07, Thomas 'PointedEars' Lahn wrote:
>>> Aleksandro wrote:
>>>> On 27/03/16 17:44, Thomas 'PointedEars' Lahn wrote:
>>>>> Aleksandro wrote:
>>>>>> On 24/03/16 16:30, Thomas 'PointedEars' Lahn wrote:
>>>>>>>   var myFunc;
>>>>>>>   myFunc = function () { alert("This is my anonymous function"); };
>>>>>>>
>>>>>>> (In that case, “myFunc” is assigned the “undefined” value first.)
>>>>>> Would the VM actually assign undefined first? I doubt it.
>>>>> On what grounds?
>>>> Well, there's a thing called optimization... you know...
>>> Although successive lines, they are not necessarily processed so.
>>
>> But they might.
> 
> I am not convinced.
> 
> There are at least four points that you are overlooking here:
> 
> 1. It was an *example* demonstrating functional equivalence of lines of
>    source code.
> 
> 2. myFunc = function () { alert("This is my anonymous function"); };
>    var myFunc;
>    
>    is equivalent to the previous code, too.
> 
> 3. There can be source code between declaration and assignment, and in the 
>    assignment, referring to the variable.  The value of the variable has to
>    be the “undefined” value then.

That is a totally different scenario that will most certainly be
optimized as well.

> 4. There is not only one virtual machine.  It need not even *be* a *virtual*
>    machine; for example, Google V8 JIT-compiles source code directly into 
>    machine code.
> 
>    <https://developers.google.com/v8/design>

And that JIT-compiler can make the same inferences.

> Taking unnecessary risks based on wild guesses and wishful thinking is the 
> mark of a wannabe.

[toc] | [prev] | [standalone]


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


csiph-web