`
hongtoushizi
  • 浏览: 361383 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

Functions -jquery

阅读更多

转载:http://stage.learn.jquery.com/javascript-101/functions/#immediately-invoked-function-expression-iife

 

Functions

Functions contain blocks of code that need to be executed repeatedly. Functions can take zero or more arguments, and can optionally return a value.

Functions 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.
}
1
2
3
4
5
// Named function expression.
 
var foo = function() {
// Do something.
};

linkUsing Functions

1
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"
1
2
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"
1
2
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"

linkImmediately-Invoked Function Expression (IIFE)

A common pattern in JavaScript is the immediately-invoked function expression. This pattern creates a function expression and then immediately executes the function. This pattern is extremely useful for cases where you want to avoid polluting the global namespace with code – no variables declared inside of the function are visible outside of it.

1
2
3
4
5
6
7
// An immediately-invoked function expression.
 
(function() {
var foo = "Hello world";
})();
 
console.log( foo ); // undefined!

linkFunctions as Arguments

In JavaScript, functions are "first-class citizens" – they can be assigned to variables or passed to other functions as arguments. Passing functions as arguments is an extremely common idiom in jQuery.

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";
});
1
2
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"
分享到:
评论

相关推荐

    前端项目-jquery.finderselect.zip

    前端项目-jquery.finderselect,Add File Explorer selecting functions to any element

    jQuery.in.Action.3rd.Edition.161729207

    Chapter 9 Beyond the DOM with jQuery utility functions Chapter 10 Talk to the server with Ajax Chapter 11 Demo: an Ajax-powered contact form Part 3 Advanced topics Chapter 12 When jQuery is not ...

    jquery电子文档chm

    All subsequent clicks continue to rotate through the two functions. Use unbind("click") to remove. 返回值 jQuery 参数 fn (Function) : 第奇数次点击时要执行的函数。 fn (Function) : 第偶数次点击时要...

    jQuery完全实例.rar

    jQuery1.2 API 中文版折叠展开折叠全部展开全部 英文说明 核心jQuery 核心函数 jQuery(expression,[context]) jQuery(expression,[context]) 这个函数接收一个包含 CSS 选择器的字符串,然后用这个字符串去匹配一组...

    jQuery.Essentials.

    It acts through JavaScript to ascribe HTML elements to the DOM attributes. Because it is a library of predefined functions, all you need to start using jQuery is a working knowledge of the syntax and...

    ondomready:一个AMD兼容模块,用于检测DOM准备就绪的时间。 基于jQuery的源代码-jquery source code

    // Functions passed to onDomReady will be executed as soon as the DOM is ready. // Execute this function ASAP onDomReady ( function ( ) { // Your code } ) ; // Define a callback var init = function ...

    Pro PHP and JQuery

    view, edit, and delete events., * Use jQuery to allow the calendar app to be viewed and edited without requiring page refreshes using built-in AJAX functions., * Learn the power and versatility of ...

    Beyond jQuery(Apress,2016)

    Cutting edge web and JavaScript specifications, such as the Fetch API, Promises, WeakMap, and Async Functions, are also discussed. Check out the comprehensive web application at github....

    Beyond.jQuery.1484222342

    Beyond jQuery gives you the confidence to abandon your jQuery crutches and walk freely with the power of the "web API" and JavaScript! Learn about the most important concepts surrounding web ...

    CodingDojo-webFun_jQuery_Functions

    CodingDojo-webFun_jQuery_Functions

    推荐jQuery的好书jQuery in Action(part2)

    英文版,共377页,有许多实例 章节目录: 1 Introducing jQuery ...6 jQuery utility functions 7 Extending jQuery with custom plugins 8 Talk to the server with Ajax 9 Prominent, powerful,and practical plugins

    2010 Pro PHP and jQuery

    Use jQuery to allow the calendar app to be viewed and edited without requiring page refreshes using built-in AJAX functions. Learn the power and versatility of PHP’s object-oriented programming ...

    jQuery 1.5 API 中文版

    Data functions $.clearQueue( [name] ) $.dequeue( [name] ), jQuery.dequeue( [name] ) objjQuery.data( element, key ), jQuery.data( ) obj.data( ), .data( key ) $.data( key, val ), .data( obj ) $.remove...

    Pro PHP and jQuery

    * Learn to utilize built-in PHP functions to build calendar tools and photo galleries. * Learn how jQuery can be used for Ajax, animation, client-side validation, and more. * Learn jQuery UI and ...

    Wrox.Professional.jQuery 2012

    Chapter 3 introduces the basic functions that make up the library and illuminates usages of the core jQuery functions. It also introduces many of the functions that you will need to perform varieties...

    jQuery详细教程

    --- jQuery functions go here ---- }); 这是为了防止文档在完全加载(就绪)之前运行 jQuery 代码。 二. jQuery 选择器 在前面的章节中,我们展示了一些有关如何选取 HTML 元素的实例。 关键点是学习 jQuery 选择...

    Another JQuery Grid Plugin —— MagicGrid 插件

    MagicGrid is a small and flexible JQuery grid plugin. Although it only provide basic grid function, good design model give it great flexibility. It is open source and totally free. Before your start, ...

    bitter:龙舌兰酒框架的伟大重构

    模块管理:RequireJS with jQuery ( ) 用户可以通过define API 定义一个模块,并通过require API 包含一个模块。 所有模块都由其路径引用 For example there is a directory in the framework root | |---basic | |...

    推荐jQuery的好书Manning jQuery in Action(part 1)

    英文版,共377页,有许多实例 章节目录: 1 Introducing jQuery ...6 jQuery utility functions 7 Extending jQuery with custom plugins 8 Talk to the server with Ajax 9 Prominent, powerful,and practical plugins

    Web Developer's Reference Guide

    side scripting in your web applicationsDiscover new ways to develop your website's front end quickly and easily using BootstrapWrite JavaScript extensibly using jQuery-JavaScript's feature-rich ...

Global site tag (gtag.js) - Google Analytics