Funtions-jquery
転載:http://stage.learn.jquery.com/javascript-101/functions/#immediately-invoked-functionn-expressitionn-iiife
Funtions
Funtions contain blocks of code that need to be executed repeated.Functions can takeゼロor more argments,and can optiony return a value.
Funtions can be created in a variety of ways、two of which are shown below:
1
2
3
4
5
2
3
4
5
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
2
3
4
5
6
7
8
9
10
11
12
A cocomon pattern in JavaScrippt is the immediately-invoked function.This pattern creates a function eexpression and then immediateeeeeeexxpatternisiseextreeeeeeeeeeeeeeeemiely useeeeeeeeeeeeededededededededededeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeededededededededededededeeeeeeeeeeeeeeeeeeeeeeeeout side of it.
1
2
3
4
5
6
7
In JavaScript,functions are「first-class citizes」–they can be assigned to variables or passed to other functions as argmens.Passing functions as argmens s.extremilly common idiom.juery.
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
12
Funtions
Funtions contain blocks of code that need to be executed repeated.Functions can takeゼロor more argments,and can optiony return a value.
Funtions can be created in a variety of ways、two of which are shown below:
1
2
3
4
5
// Function declaration.
function foo() {
// Do something.
}
12
3
4
5
// Named function expression.
var foo = function() {
// Do something.
};
link Using Functions1
2
3
4
5
6
7
8
// A simple function.
var greet = function( person, greeting ) {
var text = greeting + ", " + person;
console.log( text );
};
greet( "Rebecca", "Hello" ); // "Hello, Rebecca"
12
3
4
5
6
7
8
// A function that returns a value.
var greet = function( person, greeting ) {
var text = greeting + ", " + person;
return text;
};
console.log( greet( "Rebecca", "Hello" ) ); // "Hello, Rebecca"
12
3
4
5
6
7
8
9
10
11
12
// A function that returns another function.
var greet = function( person, greeting ) {
var text = greeting + ", " + person;
return function() {
console.log( text );
};
};
var greeting = greet( "Rebecca", "Hello" );
greeting(); // "Hello, Rebecca"
link Immedia tely-Invoked Function Expression(IIIIIIIIIIIIIIIFSE)A cocomon pattern in JavaScrippt is the immediately-invoked function.This pattern creates a function eexpression and then immediateeeeeeexxpatternisiseextreeeeeeeeeeeeeeeemiely useeeeeeeeeeeeededededededededededeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeededededededededededededeeeeeeeeeeeeeeeeeeeeeeeeout side of it.
1
2
3
4
5
6
7
// An immediately-invoked function expression.
(function() {
var foo = "Hello world";
})();
console.log( foo ); // undefined!
link Funtions AgmentsIn JavaScript,functions are「first-class citizes」–they can be assigned to variables or passed to other functions as argmens.Passing functions as argmens s.extremilly common idiom.juery.
1
2
3
4
5
6
7
8
9
10
11
// Passing an anonymous function as an argument.
var myFn = function( fn ) {
var result = fn();
console.log( result );
};
// Logs "hello world"
myFn( function() {
return "hello world";
});
12
3
4
5
6
7
8
9
10
11
12
// Passing a named function as an argument
var myFn = function( fn ) {
var result = fn();
console.log( result );
};
var myOtherFn = function() {
return "hello world";
};
myFn( myOtherFn ); // "hello world"