


Share the strange writing method of JS to determine whether the element is a number_javascript skills
This is what I saw when reading the source code of underscore (1.3.3). Its each method
var each = _.each = _.forEach = function(obj, iterator, context) {
if (obj == null) return;
if (nativeForEach && obj.forEach === nativeForEach) {
obj.forEach(iterator, context);
} else if (obj.length === obj.length) {
for (var i = 0, l = obj. length; i < l; i ) {
if (iterator.call(context, obj[i], i, obj) === breaker) return;
}
} else {
for (var key in obj) {
if (_.has(obj, key)) {
if (iterator.call(context, obj[key], key, obj) === breaker) return;
}
}
}
};
There is a sentence in this method
I didn’t understand it after looking at it for a long time. After being pointed out by an expert, this sentence is equivalent to
is used to determine whether the element is of numeric type. typeof and Object.prototype.toString are common ways of writing. The last one is uncommon and difficult for ordinary people to understand.
Some libraries have tool functions for type judgment, such as
function isNumber1(a){
return typeof a === 'number'
}
Or use Object.prototype.toString
function isNumber2(a) {
return Object.prototype.toString.call(a ) === '[object Number]'
}
Changed to this way
function isNumber3(a){
return a === a
}
Test with various types
var arr = ['1', true, false, undefined, null, {}, [], 1]
for (var i=0; i
}
As a result, only the last item in the array is true. That is, only numeric type a === a is true.
Why not use typeof, because string comparison theoretically needs to traverse all characters, and the performance is directly proportional to the length of the string.

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this article, we will discuss the problem of finding numbers between 1 and n (given) that are not divisible by any number between 2 and 10. Let us understand this with some examples - Input:num=14Output:3Explanation:Therearethreenumbers,1,11,and13,whicharenotdivisible.Input:num=21Output:5Explanation:Therearefivenumbers1,11,13,17,and19,whicharenotdivisible. Solved Simple method if

CSS transition effect: How to achieve the sliding effect of elements Introduction: In web design, the dynamic effect of elements can improve the user experience, among which the sliding effect is a common and popular transition effect. Through the transition property of CSS, we can easily achieve the sliding animation effect of elements. This article will introduce how to use CSS transition properties to achieve the sliding effect of elements, and provide specific code examples to help readers better understand and apply. 1. Introduction to CSS transition attribute transition CSS transition attribute tra

In mathematics, the divisibility rule of 5 states that if a number ends in 0 or 5, it is divisible by 5. There is another way to determine the divisibility rule of 5, if the remainder is 0, then return the number divisible by 5. The mod(%) operator is commonly used in programming for integer division. Let's give an example. The given number is 525, the number ends with 5 and is divisible by 5. The given number is 7050 which ends with 0 and is divisible by 5. The given number is 678 which does not end with 0 and 5 and is not divisible by 5. In this article, we will solve the question of whether the number is divisible by 5. Algorithm The following steps are where we will use the java.util.* packages to get user input of primitive data types. from main class

Methods to restore the WeChat corner number: 1. Force quit WeChat and restart; 2. Clear the WeChat cache; 3. Check for WeChat version updates; 4. Uninstall and reinstall WeChat. Detailed introduction: 1. Force quit WeChat and restart. This is the most common method to solve the abnormal number of WeChat corner mark. In the WeChat interface, click the "Me" button in the lower left corner, and then click the "Settings" button in the upper right corner. Open the settings interface. In the settings interface, select "Log out" to log out of WeChat. After a few seconds, start WeChat again. Normally, the corner number will return to normal, etc.

CSS transformation: How to achieve the rotation effect of elements requires specific code examples. In web design, animation effects are one of the important ways to improve user experience and attract user attention, and rotation animation is one of the more classic ones. In CSS, you can use the "transform" attribute to achieve various deformation effects of elements, including rotation. This article will introduce in detail how to use CSS "transform" to achieve the rotation effect of elements, and provide specific code examples. 1. How to use CSS’s “transf

How to use CSS to achieve the transparency gradient effect of elements In web development, adding transition effects to web page elements is one of the important means to improve user experience. The gradient effect of transparency can not only make the page smoother, but also highlight the key content of the element. This article will introduce how to use CSS to achieve the transparency gradient effect of elements and provide specific code examples. Using the CSS transition attribute To achieve the transparency gradient effect of an element, we need to use the CSS transition attribute. t

In the intricate web of digital life, our Apple ID is the gateway to our personal and digital assets, seamlessly connecting us to our iPhone, iPad, Mac, iCloud, App Store and iTunes Store. However, the daunting prospect of forgetting your Apple ID password can cast a shadow on our digital freedom, hindering access to our valuable data, blocking app downloads, and ruining our online experience. Don’t worry, as there are ways to take back control of your account and restore your digital balance. Navigating the Password Reset Process The first step in this endeavor is to embark on a journey to the official AppleID account website, which is a place where you can solve your password effortlessly.

The top four global virtual currency trading platforms in 2025 are: Binance: a leader in the industry, providing diversified trading options and innovative products. OKX: A huge user base, providing comprehensive cryptocurrency services. Gate.io: User-friendly, offering a wide range of cryptocurrency options. Bitget: Focus on derivatives trading and provides high leverage futures contracts.
