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


Groups > comp.lang.javascript > #23816

Re: Defining functions in JavaScript

From Andreas Bergmaier <andber93@web.de>
Newsgroups comp.lang.javascript
Subject Re: Defining functions in JavaScript
Date 2014-04-07 18:00 +0200
Organization albasani.net
Message-ID <lhui34$v6e$1@news.albasani.net> (permalink)
References <1d82e037-4811-4a45-9668-985bdac67e45@googlegroups.com>

Show all headers | View raw


AbdulFattah Popoola schrieb:

> issues developers should know about when using the various styles.

You haven't named http://kangax.github.io/nfe/ though?

> http://abdulapopoola.com/2014/04/03/defining-javascript-functions/
>
> Kindly post your feedback.

Some inaccurracies/mistakes:

* "Creating a function expression is easy: just declare a function where 
the parser expects an expression and it automatically becomes a function 
expression."

The term "declare" might be confusing here, it also indicates a name 
which is optional for function expressions.

* "Function expressions are essential to achieve functional programming 
concepts like currying, partials in JavaScript."

They are a fundamental part in the expressiveness of JavaScript, but 
they're not *necessary* for functional programming. You could do 
everything with function declarations only.

* "The last way is to use the Function keyword itself, this constructs a 
function object and returns it."

No. The "function" keyword is what opens a function declaration or 
function expression, you are referring to the "Function" *constructor* 
function here. It might be a built-in global, but it's not a keyword.

* "the function body has to be parsed every time it is called."

A body isn't called, a function is. Because of that, the statement is 
confusing; you should emphasize that the function body only needs to be 
parsed when the Function constructor is called, not every time when the 
resulting function is called and the body is executed.

* "And there are potential risks from malicious agents"

Those come from *unsafe* construction of dynamic code strings, not from 
using Function() in general.

* "However for function expressions, the variable declaration is hoisted"

Not all function expressions appear inside a variable initialisation.

*   if(true) {
         //function expression because
         //it is not a source element
         function baz() {};
     }

Nope, that's a syntax error. Expression statements must not begin with 
the function keyword. See 
http://kangax.github.io/nfe/#function-declarations-in-blocks for more 
information

* "Here are some more examples of queer ways to create functions:"
     eval('f("eval")');
     setTimeout('f("timeout")',1000);

Those aren't really creating function objects, they're just evaling 
code. You could however do

     var f = eval("(function(str) { alert(str); })");


Regards,
  Bergi

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Defining functions in JavaScript AbdulFattah Popoola <abdulapopoola@gmail.com> - 2014-04-06 08:19 -0700
  Re: Defining functions in JavaScript Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2014-04-07 08:58 +0200
    Re: Defining functions in JavaScript "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-04-07 13:43 +0200
  Re: Defining functions in JavaScript Andreas Bergmaier <andber93@web.de> - 2014-04-07 18:00 +0200
    Re: Defining functions in JavaScript Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-04-07 19:26 +0200

csiph-web