`
hongtoushizi
  • 浏览: 376506 次
  • 性别: 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"
分享到:
评论

相关推荐

    前端CSS-JS-jQuery的API-js库2个版本

    `jquery.api.3.2.1.chm`和`jquery-3.6.0.js`、`jquery-3.6.0.min.js`文件与jQuery相关,其中`.api.3.2.1.chm`可能是jQuery 3.2.1版本的API参考手册,而`.js`和`.min.js`则是库的完整和压缩版本。jQuery提供了一套...

    jquery实例 涵盖jquery所有基本用法

    9. **实用函数(Utility Functions)** - `$.each()`, `$.map()`, `$.inArray()`, `$.trim()`等实用函数提供了数组、对象处理和字符串操作等功能。 10. **兼容性(Compatibility)** - jQuery致力于跨浏览器兼容性...

    JQuery(A~Z)教程

    ### F - Functions jQuery函数是其核心组成部分,如事件处理函数、动画函数等。`.click()`, `.hover()`, `.change()`等用于绑定事件,`.each()`用于遍历数组或对象,`.ready()`确保DOM加载完成后执行指定的函数。 ...

    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-3.3.1、JavaScript API中文文档

    3. **函数(Functions)** - 函数可以作为一等公民,可以赋值给变量,作为参数传递,也可以作为返回值。ES6引入的箭头函数提供更简洁的语法。 4. **原型与继承(Prototype & Inheritance)** - JavaScript使用...

    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:IE失去了浏览器之战,该继续前进了。 感谢您的服务jQuery:folded_hands:

    ES6,即ECMAScript 2015,是JavaScript语言的一个重要更新,引入了许多新的特性,比如类(Classes)、箭头函数(Arrow Functions)、模板字符串(Template Literals)、解构赋值(Destructuring)、let和const声明...

    JQuery教程全集

    - **Utility Functions**:JQuery 提供了一系列实用函数,用于执行常见的任务,例如获取或设置 CSS 属性、动画效果、事件处理等。 - **Document Ready Handler**:确保在文档加载完成后才执行 JavaScript 代码,避免...

    JQUERY的API中文文档

    8. **实用函数(Utility Functions)** - `$.each()`: 遍历数组或对象。 - `$.trim()`: 去除字符串两端的空白字符。 - `$.inArray()`: 检查元素是否在数组中。 - `$.isFunction()`: 检查对象是否为函数。 9. **...

    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...

    基于Jquery可居中显示并兼容IE6+/FF等浏览器的瀑布流程序(jquery.easing.js,jquery.vgrid.js实现)

    2. jquery.easing.js:这是一个Jquery插件,提供了丰富的缓动函数(easing functions),用于创建平滑的动画效果。在瀑布流布局中,当用户滚动页面时,新加载的元素可能会通过动画形式平滑地进入视口,增加用户体验...

    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 ...

    JQuery In Action.PDF

    - **Utility Functions:** JQuery includes a set of utility functions that simplify common tasks such as manipulating CSS styles, adding classes, or applying animations. - **Example:** `.addClass(...

    CodingDojo-webFun_jQuery_Functions

    【标题】"CodingDojo-webFun_jQuery_Functions" 是一个关于使用jQuery库进行Web开发的实践项目。在这个项目中,我们将深入学习jQuery的各种函数,这些函数极大地简化了JavaScript中的DOM操作、事件处理和动画效果。 ...

    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....

    jQuery-in-Action.part2.pdf

    在深入探讨`jQuery-in-Action.part2.pdf`的第六章之前,我们先回顾一下jQuery的基本概念。jQuery是一个流行的JavaScript库,它极大地简化了HTML文档的遍历、事件处理、动画以及与服务器的交互。本书的第六章主要介绍...

    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 ...

    jquery图片轮流滚动展示

    **`jquery-easing-1.3`插件**是jQuery的一个扩展,提供了更多的缓动函数(easing functions),用于控制动画的速度变化。这些缓动函数可以让动画更加丰富多样,比如线性、抛物线、回弹、加速、减速等效果。在图片...

Global site tag (gtag.js) - Google Analytics