[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.javascript

Can somebody explain this code

Tony Johansson

3/23/2016 9:51:00 PM

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

9 Answers

Thomas 'PointedEars' Lahn

3/24/2016 7:30:00 PM

0

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-sta...
<http://www.ecma-international.org/ecma-262/6.0/#sec-function-instance...

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

Aleksandro

3/24/2016 11:20:00 PM

0

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-sta...
> <http://www.ecma-international.org/ecma-262/6.0/#sec-function-instance...

Thomas 'PointedEars' Lahn

3/27/2016 8:44:00 PM

0

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

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

Aleksandro

3/27/2016 9:23:00 PM

0

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

Thomas 'PointedEars' Lahn

3/27/2016 11:08:00 PM

0

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

Aleksandro

3/27/2016 11:26:00 PM

0

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.

Thomas 'PointedEars' Lahn

5/4/2016 3:52:00 AM

0

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

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

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

5/4/2016 6:55:00 AM

0

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)

Aleksandro

5/7/2016 4:03:00 PM

0

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

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.