summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAdam Brenecki <adam@brenecki.id.au>2013-08-14 17:21:29 +0930
committerAdam Brenecki <adam@brenecki.id.au>2013-08-14 17:21:29 +0930
commita82859f95bb235074d740530dfa60afca7025223 (patch)
tree2873b7ecbcbfc04c9cd7f7f2e702d1e18a8abc7b
parent02ea95377d4963c60a96976b4092bdcc2b638594 (diff)
Explain anonymous functions after first-class, and more concisely
-rw-r--r--javascript.html.markdown19
1 files changed, 8 insertions, 11 deletions
diff --git a/javascript.html.markdown b/javascript.html.markdown
index 1f188832..bcaf9a29 100644
--- a/javascript.html.markdown
+++ b/javascript.html.markdown
@@ -211,16 +211,6 @@ function myFunction(thing){
}
myFunction("foo") // = "FOO"
-// Functions can also be defined "anonymously" - without a name:
-(function(thing){
- return thing.toLowerCase()
-})
-// Note: Stand-alone function declarations require an identifier name.
-// Anonymous functions are values, not declarations, so they must be
-// treated as a value. We wrap ours here in ( ) to do so, or you could assign
-// it to a variable, or pass it as a parameter to another function.
-// (we can't call our function, since we don't have a name to refer to it with)
-
// JavaScript functions are first class objects, so they can be reassigned to
// different variable names and passed to other functions as arguments - for
// example, when supplying an event handler:
@@ -229,9 +219,16 @@ function myFunction(){
}
setTimeout(myFunction, 5000)
+// Functions can also be defined "anonymously" - without a name:
+var lowerFunction = function(thing){
+ return thing.toLowerCase()
+}
+lowerFunction("Foo") // = "foo"
+// (note: we've assigned our anonymous function to a variable - if we didn't, we
+// wouldn't be able to access it)
+
// You can even write the function statement directly in the call to the other
// function.
-
setTimeout(function myFunction(){
// this code will be called in 5 seconds' time
}, 5000)