


How to use tools in jquery $.isFunction, $.isArray(), $.isWindow()_jquery
In Judgement of variable type in JavaScript, we explained the principle of $.type() implementation in jquery. Of course, in addition to providing $.type tool methods, jquery also provides several other tool methods: $.isFunction(), $.isArray(), $.isWindow(), $.isNumeric(), etc. The purpose of these methods can be seen from the method names. Let's explain the internal details of these methods implemented in jQuery (2.1.2) one by one.
1. $.isFunction()
$.isFunction() is used to determine whether a variable is of function type. Let’s take a look at a few examples:
$.isFunction(123); // false $.isFunction(true);// false $.isFunction([1, 2]);// false $.isFunction(function(){});// true function func(){ } var sfunc = function(){ } $.isFunction(func); // true $.isFunction(sfunc);// true
As you can see from the above example, in $.isFunction(param), if the incoming param is a function type, it will return true; for other types, it will return false.
Looking at the source code of jquery, we can see that $.isFunction() is also implemented through $.type():
isFunction: function( obj ) { return jQuery.type(obj) === "function"; }
2. $.isArray()
$.isArray() is used to determine whether the variable is of array type. Similarly, we also look at the usage of $.isArray through several examples:
$.isArray(123); // false $.isArray(true); // false $.isArray([1, 2]);// true $.isArray(new Array(3, 4)); // true
Whether it is an array literal or a variable created using the new keyword, you can use $.isArray() to determine whether it is an array type. In the jquery source code, $.isArray calls the isArray method provided by native Array. Because in higher versions of browsers, native JavaScript has been provided with an isArray method to determine whether the variable is of array type.
isArray: Array.isArray
3. $.isWindow()
$.isWindow() is used to determine whether the current variable is window, such as:
$.isWindow(window); // true $.isWindow([]); // false $.isWindow(null); // false
In jQuery source code:
isWindow: function( obj ) { return obj != null && obj === obj.window; }
He determines whether obj is a window object by determining whether obj has a window attribute. Because there is an attribute window in the window object, which is itself, therefore: window.window===window, the same:
window.window.window.window === window;
You can keep looping.
Why do we need to first determine whether obj is null in the code? Because when judging whether null or undefined has a window property, the code will throw an exception: Uncaught TypeError: Cannot read property 'window' of null. Therefore, in order to prevent code errors, first determine whether the variable is null. If it is null, it is definitely not a window object and returns false directly; otherwise, it is then determined whether the variable has a window attribute.
4. $.isNumeric()
$.isNumeric() is used to determine whether the current variable is of numeric type, but why don’t I use $.type()=="number" to determine. Let’s take a look at some official examples first:
$.isNumeric("-10"); // true $.isNumeric(16); // true $.isNumeric(0xFF); // true $.isNumeric("0xFF"); // true $.isNumeric("8e5"); // true (exponential notation string) $.isNumeric(3.1415); // true $.isNumeric(+10); // true $.isNumeric(0144); // true (octal integer literal) $.isNumeric(""); // false $.isNumeric({}); // false (empty object) $.isNumeric(NaN); // false $.isNumeric(null); // false $.isNumeric(true); // false $.isNumeric(Infinity); // false $.isNumeric(undefined); // false
Using $.isNumeric() can determine string type numbers such as "-10" and "0xFF", while $.type() will parse them into string type.
In the jquery source code, the variable type is determined like this:
isNumeric: function( obj ) { // parseFloat NaNs numeric-cast false positives (null|true|false|"") // ...but misinterprets leading-number strings, particularly hex literals ("0x...") // subtraction forces infinities to NaN // adding 1 corrects loss of precision from parseFloat (#15100) return !jQuery.isArray( obj ) && (obj - parseFloat( obj ) + 1) >= 0; }
First determine whether the variable is of array type, and if so, return false directly. But why do we need to first determine whether the variable is of array type? Because arrays of type [123] can be directly subtracted, and can also be converted to numbers through parseFloat(["123"]):
[100] - 60 // 40 [100] - [60] // 40 parseFloat([123]) // 123 parseFloat(["345"]) // 345
Therefore, it cannot be converted directly through parseFloat() and then judged. First, you must determine whether the variable is an array; if not, proceed to the next step:
(obj - parseFloat( obj ) 1) >= 0
Pure numbers, string type numbers, numbers starting with 0 (octal), arrays starting with 0x (hexadecimal), etc. can be converted to decimal numbers through parseFloat(). After the operation of the above expression, it must be greater than 0. But why add 1? It is also explained in the code that converting through parseFloat() will cause the problem of loss of precision, so after 1, the operation result will be more accurate.
For other types, NaN is obtained after conversion through parseFloat(). No matter what operation is performed, NaN cannot be compared with 0 and returns false.
In previous versions of jquery (such as 2.0.2):
isNumeric: function( obj ) { return !isNaN( parseFloat(obj) ) && isFinite( obj ); }
We can find that after running the code $.isNumeric([123]), we get true, but in fact, it is an array type. Fortunately, it has been fixed in subsequent versions.
5. $.isEmptyObject()
$.isEmptyObject() is not used to determine the type of a variable, but to determine whether an object type is empty and does not contain any attributes.
Starting with jQuery 1.4, this method detects both properties on the object itself and properties inherited from the prototype (so hasOwnProperty is not used). The parameter should be a plain JavaScript object, other types of objects (DOM elements, raw strings/numbers, host objects) may not provide consistent results across browsers.
$.isEmptyObject({name:"wenzi"}) // false $.isEmptyObject({}) // true function Person(){ this.name = "wenzi" } $.isEmptyObject(new Person()); // false function Student(){ } Student.prototype.name = "wenzi"; $.isEmptyObject(new Student()); // false
我们能够看到,不论是对象本身的属性,还是prototype上的属性,只要存在,都会返回false。
isEmptyObject: function( obj ) { var name; for ( name in obj ) { return false; } return true; }
在jquery中,是通过for~in进行检测的。因为for~in也是能循环到prototype上的属性的,若进入到循环中,则说明obj存在属性,发挥false;否则返回true。
6. 总结
jquery中还提供了很多各种各样的工具方法,让我们在编写js代码时更加的方便。以后有机会时再总结其他的工具方法。

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.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software