Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 04cce1f

Browse files
authored
Anonymous Functions
1 parent 4417750 commit 04cce1f

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

‎README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,54 @@ function countUp(n) {
163163
countUp(1);
164164
```
165165

166+
## Anonymous Functions
167+
168+
Anonymous function is the function that was declared without any named identifier to refer to it.
169+
170+
For example:
171+
172+
```javascript
173+
// Normal function
174+
function sayHi() {
175+
alert('Hello world!');
176+
}
177+
sayHi();
178+
179+
// Anonymous function assigned to sayHi variable
180+
var sayHi = function() {
181+
alert('Hello World!');
182+
}
183+
184+
// Use as an argument to other functions
185+
setTimeout(function() {
186+
console.log('Hello World!');
187+
}, 1000);
188+
189+
// Use as a closure
190+
(function() {
191+
alert('Hello World!');
192+
})();
193+
194+
// Use as a closure with parameters
195+
(function(msg) {
196+
alert(msg);
197+
}('Hello World!'));
198+
199+
// An anonymous function can refer to itself via arguments.callee local variable, useful for recursive anonymous functions
200+
console.log(
201+
(function(n) {
202+
return (1 < n) ? arguments.callee(n - 1) + n : 1;
203+
})(10)
204+
);
205+
206+
// Instead of using arguments.callee, you can use named function expression instead
207+
console.log(
208+
(function sum(n) {
209+
return (n <= 1) ? 1 : sum(n-1) + n
210+
})(10)
211+
);
212+
```
213+
166214
## Currying
167215

168216
Currying allows a function with multiple arguments to be translated into a sequence of functions. Curried functions can be tailored to match the signature of another function.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /