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.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提供了一套...
9. **实用函数(Utility Functions)** - `$.each()`, `$.map()`, `$.inArray()`, `$.trim()`等实用函数提供了数组、对象处理和字符串操作等功能。 10. **兼容性(Compatibility)** - jQuery致力于跨浏览器兼容性...
### F - Functions jQuery函数是其核心组成部分,如事件处理函数、动画函数等。`.click()`, `.hover()`, `.change()`等用于绑定事件,`.each()`用于遍历数组或对象,`.ready()`确保DOM加载完成后执行指定的函数。 ...
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 ...
3. **函数(Functions)** - 函数可以作为一等公民,可以赋值给变量,作为参数传递,也可以作为返回值。ES6引入的箭头函数提供更简洁的语法。 4. **原型与继承(Prototype & Inheritance)** - JavaScript使用...
All subsequent clicks continue to rotate through the two functions. Use unbind("click") to remove. 返回值 jQuery 参数 fn (Function) : 第奇数次点击时要执行的函数。 fn (Function) : 第偶数次点击时要...
jQuery1.2 API 中文版折叠展开折叠全部展开全部 英文说明 核心jQuery 核心函数 jQuery(expression,[context]) jQuery(expression,[context]) 这个函数接收一个包含 CSS 选择器的字符串,然后用这个字符串去匹配一组...
ES6,即ECMAScript 2015,是JavaScript语言的一个重要更新,引入了许多新的特性,比如类(Classes)、箭头函数(Arrow Functions)、模板字符串(Template Literals)、解构赋值(Destructuring)、let和const声明...
- **Utility Functions**:JQuery 提供了一系列实用函数,用于执行常见的任务,例如获取或设置 CSS 属性、动画效果、事件处理等。 - **Document Ready Handler**:确保在文档加载完成后才执行 JavaScript 代码,避免...
8. **实用函数(Utility Functions)** - `$.each()`: 遍历数组或对象。 - `$.trim()`: 去除字符串两端的空白字符。 - `$.inArray()`: 检查元素是否在数组中。 - `$.isFunction()`: 检查对象是否为函数。 9. **...
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...
2. jquery.easing.js:这是一个Jquery插件,提供了丰富的缓动函数(easing functions),用于创建平滑的动画效果。在瀑布流布局中,当用户滚动页面时,新加载的元素可能会通过动画形式平滑地进入视口,增加用户体验...
// 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 ...
- **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" 是一个关于使用jQuery库进行Web开发的实践项目。在这个项目中,我们将深入学习jQuery的各种函数,这些函数极大地简化了JavaScript中的DOM操作、事件处理和动画效果。 ...
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的基本概念。jQuery是一个流行的JavaScript库,它极大地简化了HTML文档的遍历、事件处理、动画以及与服务器的交互。本书的第六章主要介绍...
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-easing-1.3`插件**是jQuery的一个扩展,提供了更多的缓动函数(easing functions),用于控制动画的速度变化。这些缓动函数可以让动画更加丰富多样,比如线性、抛物线、回弹、加速、减速等效果。在图片...