Home Web Front-end JS Tutorial Share the strange writing method of JS to determine whether the element is a number_javascript skills

Share the strange writing method of JS to determine whether the element is a number_javascript skills

May 16, 2016 pm 05:51 PM
element number

This is what I saw when reading the source code of underscore (1.3.3). Its each method

Copy the code The code is as follows:

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
Copy code The code is as follows:
if (obj.length === obj.length)

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
Copy code The code is as follows:
if (typeof obj.length === 'number')

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
Copy code The code is as follows:

function isNumber1(a){
return typeof a === 'number'
}

Or use Object.prototype.toString
Copy code The code is as follows:

function isNumber2(a) {
return Object.prototype.toString.call(a ) === '[object Number]'
}

Changed to this way
Copy code The code is as follows:

function isNumber3(a){
return a === a
}

Test with various types
Copy code The code is as follows:

var arr = ['1', true, false, undefined, null, {}, [], 1]
for (var i=0; iconsole.log(isNumber3(arr[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.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Find numbers that are not divisible by any number in a range, using C++ Find numbers that are not divisible by any number in a range, using C++ Sep 13, 2023 pm 09:21 PM

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 CSS transition effect: how to achieve the sliding effect of elements Nov 21, 2023 pm 01:16 PM

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

Java program to check if a number is divisible by 5 Java program to check if a number is divisible by 5 Sep 13, 2023 pm 09:01 PM

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

How to restore WeChat corner mark numbers How to restore WeChat corner mark numbers Nov 29, 2023 pm 05:46 PM

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 CSS transformation: how to achieve the rotation effect of elements Nov 21, 2023 pm 06:36 PM

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 an element's transparency gradient effect How to use CSS to achieve an element's transparency gradient effect Nov 21, 2023 pm 01:38 PM

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

What to do if you forget your Apple ID password What to do if you forget your Apple ID password Dec 19, 2023 pm 05:04 PM

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.

What are the platforms for virtual currency trading around the world? The top ten latest 2025 digital currency app rankings What are the platforms for virtual currency trading around the world? The top ten latest 2025 digital currency app rankings Feb 27, 2025 pm 06:09 PM

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.

See all articles