R
Ross McKay
OK, this is different: apparently, the function declaration is now seen
as an anti-pattern. This doesn't sit comfortably with me; can the
resident experts please weigh in and discuss?
I can understand Resig's logic in better enabling students to come to
grips with functions as first-class objects, but... an anti-pattern?
---
http://www.blog.highub.com/javascript/javascript-pattern-roundup-issue-2/
[...]
Function Declarations is added under General Patterns section, it was
first mentioned on John Resig's recent blog post.
// antipattern
function getData() {}
// preferred
var getData = function() { };
[...]
---
http://ejohn.org/blog/javascript-as-a-first-language/
[...]
As we've begun to look at the prospect of JavaScript-as-a-first-language
a number of obvious warts stick out (as is obvious to anyone who has
worked with JavaScript for any duration). To make sure that general
warts don't crop up we will be using some form of linting (either JSLint
or JSHint or similar) in the code editor to give the users contextual
information on what's happening with their code and why they should be
writing their code in a certain way.
We want to go beyond basic syntax tweaks though and find ways of using
the language that'll result in an easier learning experience. In
particular there are two changes which will likely result in a much
simpler on-ramp to learning.
[...]
Perhaps the most interesting change that we can make is a rather subtle
one, but it's eschewing normal function declarations for creating
anonymous functions and assigning them to a variable.
// Don't do this:
function getData() { }
// Do this instead:
var getData = function() { };
There are a number of good habits that are instilled when you use this
particular technique.
* Makes it easier to understand "functions as an object". I've found
that when you show new developers a function being assigned to a
variable it suddenly becomes much more obvious that a function is
actually an object and can be manipulated as such (and that a function
can be passed as an argument to another function). Thus students are
advanced along the path towards a better understanding of functional
programming.
* It enforces good semicolon habits. Traditional function declaration is
the only situation in which semicolons aren't needed (save for
conditional statements and loops, naturally) and it makes it much more
obvious when they're required all the time.
* Doesn't have much of the baggage traditionally associated with
functions and scope.
[...]
as an anti-pattern. This doesn't sit comfortably with me; can the
resident experts please weigh in and discuss?
I can understand Resig's logic in better enabling students to come to
grips with functions as first-class objects, but... an anti-pattern?
---
http://www.blog.highub.com/javascript/javascript-pattern-roundup-issue-2/
[...]
Function Declarations is added under General Patterns section, it was
first mentioned on John Resig's recent blog post.
// antipattern
function getData() {}
// preferred
var getData = function() { };
[...]
---
http://ejohn.org/blog/javascript-as-a-first-language/
[...]
As we've begun to look at the prospect of JavaScript-as-a-first-language
a number of obvious warts stick out (as is obvious to anyone who has
worked with JavaScript for any duration). To make sure that general
warts don't crop up we will be using some form of linting (either JSLint
or JSHint or similar) in the code editor to give the users contextual
information on what's happening with their code and why they should be
writing their code in a certain way.
We want to go beyond basic syntax tweaks though and find ways of using
the language that'll result in an easier learning experience. In
particular there are two changes which will likely result in a much
simpler on-ramp to learning.
[...]
Perhaps the most interesting change that we can make is a rather subtle
one, but it's eschewing normal function declarations for creating
anonymous functions and assigning them to a variable.
// Don't do this:
function getData() { }
// Do this instead:
var getData = function() { };
There are a number of good habits that are instilled when you use this
particular technique.
* Makes it easier to understand "functions as an object". I've found
that when you show new developers a function being assigned to a
variable it suddenly becomes much more obvious that a function is
actually an object and can be manipulated as such (and that a function
can be passed as an argument to another function). Thus students are
advanced along the path towards a better understanding of functional
programming.
* It enforces good semicolon habits. Traditional function declaration is
the only situation in which semicolons aren't needed (save for
conditional statements and loops, naturally) and it makes it much more
obvious when they're required all the time.
* Doesn't have much of the baggage traditionally associated with
functions and scope.
[...]