The jQuery.each method is one of the core tool methods of jQuery. It is a general iteration method that can be used to iterate objects and arrays. Unlike the $().each() method, which iterates over jQuery objects, this method can be used to iterate over any object. Usually requires two parameters
object: The object or array that needs to be traversed.
callback: The callback function executed by each member/element.
The callback function has two parameters: the first is the member of the object or the index of the array, and the second is the corresponding variable or content. If you need to exit the each loop, you can make the callback function return false, and other return values will be ignored.
Loop through an array, using both element index and content. Examples are as follows:
//例遍对象,同时使用成员名称和变量内容。 $.each( [0,1,2], function(i, n){ alert( "Item #" + i + ": " + n ); }); //例遍对象,同时使用成员名称和变量内容。 $.each( { name: "John", lang: "JS" }, function(i, n){ alert( "Name: " + i + ", Value: " + n ); });
Of course, you can also use the instance call directly
$( 'div' ).each( function(i,n){ return i+n.text; } )
In fact, the instance (prototype) method in the source code is also a static method called, so to analyze each method, you only need to analyze its static method. Instance calling is just a special case of using static methods.
// Execute a callback for every element in the matched set. // (You can seed the arguments with an array of args, but this is // only used internally.) each: function( callback, args ) { return jQuery.each( this, callback, args ); },
In the prototype method, pass this object directly as the object to be traversed. The following is the source code of the static method
// args is for internal usage only each: function( object, callback, args ) { var name, i = 0, length = object.length, isObj = length === undefined || jQuery.isFunction( object ); if ( args ) { if ( isObj ) { for ( name in object ) { if ( callback.apply( object[ name ], args ) === false ) { break; } } } else { for ( ; i < length; ) { if ( callback.apply( object[ i++ ], args ) === false ) { break; } } } // A special, fast, case for the most common use of each } else { if ( isObj ) { for ( name in object ) { if ( callback.call( object[ name ], name, object[ name ] ) === false ) { break; } } } else { for ( ; i < length; ) { if ( callback.call( object[ i ], i, object[ i++ ] ) === false ) { break; } } } } return object; },
There are not many things. First of all, it accepts 3 parameters. At this time, we should pay attention to the fact that the manuals we often use usually write two parameters. We generally use two parameters, but in fact, in the source code, There are 3 parameters that can be accepted, the third parameter is an array, which will be passed in as a parameter of the callback function.
First declare a few variables. i, name and length are to prepare for the loop. isObj is to distinguish whether the parameter to be convenient is an array or an object. It can be judged whether it is an object by judging whether the parameter is a function or the length attribute does not exist. , the rest are handled as arrays or array-like.
isObj = length === undefined || jQuery.isFunction( object );
This sentence is written very concisely and uses the precedence order of operators to execute first ===
In fact, this kind of judgment is not very precise, it is just a rough distinction, such as:
var obj={length:'a'}; var isObj= obj.length=== undefined || jQuery.isFunction( obj ); alert(isObj); //false
Then the distinction is based on whether the third parameter is added. Let’s first look at the situation without adding it, which is
} else { if ( isObj ) { for ( name in object ) { if ( callback.call( object[ name ], name, object[ name ] ) === false ) { break; } } } else { for ( ; i < length; ) { if ( callback.call( object[ i ], i, object[ i++ ] ) === false ) { break; } } } }
"Distinguish" arrays and objects based on the isObj variable. Use a for loop for arrays and a for...in loop for objects. Each time the loop is executed, a callback function will be executed and the current loop array or object key and value will be executed. Pass in, the call method used here, the first parameter is the "this" of the function, that is, the value of the current loop is used as this, the next two are key and value or pointer and value, so we use the callback function in the loop The second parameter is the same as using this. For example:
//刚才的例子 $( 'div' ).each( function(i,n){ return i+n.text; } ) //等价于 $( 'div' ).each( function(i,n){ return i+this.text; } )
For the case where the third parameter is added, the value transfer method of the callback function is changed. The apply method is used to transfer parameters. This still points to the current value, but args is the third parameter. The array is passed in, and the callback function can use as many parameters as the array has. What needs to be noted here is that the third one must be a js original array format and cannot be a class array or jQuery object, otherwise an error will be reported because the apply method is not supported. If the callback function returns false, the loop will be skipped. For example, we can only process the odd-numbered subscript array, and when it is judged to be an even-numbered item, just execute return false in the callback function
return object;
Finally return the original object or array, etc.
The above is the entire content of this article, I hope you all like it.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools
