首页 > 其他分享 >window.isNaN 和 Number.isNaN 对比

window.isNaN 和 Number.isNaN 对比

时间:2024-01-26 10:45:47浏览次数:16  
标签:appendChild isNaN const TR Number window TD document

 1 <!DOCTYPE html>
 2 <html lang="en">
 3     <head>
 4         <meta charset="UTF-8" />
 5         <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 6         <title>Document</title>
 7         <style>
 8             table {
 9                 margin: 200px auto;
10                 border-collapse: collapse;
11             }
12 
13             td {
14                 border: 1px solid black;
15                 padding: 5px 10px;
16             }
17         </style>
18     </head>
19     <body>
20         <div id="app">
21             <table>
22                 <thead>
23                     <tr>
24                         <th>值类型</th>
25                         <th>window.isNaN</th>
26                         <th>Number.isNaN</th>
27                         <th>对比结果</th>
28                     </tr>
29                 </thead>
30                 <tbody></tbody>
31             </table>
32         </div>
33         <script src=""></script>
34         <script>
35             const values = new Map([
36                 ['数字', 1],
37                 ['undefined', undefined],
38                 ['null', null],
39                 ['空字符串', ''],
40                 ['数字类型字符串', '1'],
41                 ['非数字类型字符串', 'a'],
42                 ['空数组', []],
43                 ['仅包含一个数字的数组', [1]],
44                 ['包含多个数字的数组', [1, 2]],
45                 ['包含非数字的数组', ['a', 'b']],
46                 ['朴素的空对象', {}],
47                 ['朴素的非空对象', { a: 1 }],
48                 ['true', true],
49                 ['false', false],
50                 ['NaN', 1 / 'a'],
51             ]);
52             const TBODY = document.querySelector('TBODY');
53             const firstFragment = document.createDocumentFragment();
54             const endFragment = document.createDocumentFragment();
55             const isNaNs = [Number.isNaN, window.isNaN];
56             values.forEach((value, key) => {
57                 const TR = document.createElement('tr');
58                 if(key === 'NaN') {
59                     TR.style.backgroundColor = '#32bfb1';
60                 }
61                 const startTD = document.createElement('td');
62                 startTD.innerText = key;
63                 TR.appendChild(startTD);
64                 let i = isNaNs.length;
65                 let isNaN = [];
66                 while (i--) {
67                     const TD = document.createElement('td');
68                     isNaN.push((TD.innerText = isNaNs[i](value)));
69                     TR.appendChild(TD);
70                 }
71                 const endTD = document.createElement('td');
72                 endTD.innerText = isNaN[0] === isNaN[1] ? '√' : '×';
73                 TR.appendChild(endTD);
74                 if (isNaN[0] === isNaN[1]) {
75                     endFragment.appendChild(TR);
76                 } else {
77                     firstFragment.appendChild(TR);
78                 }
79             });
80             TBODY.appendChild(firstFragment);
81             TBODY.appendChild(endFragment);
82             tail('推断:window.isNaN 判断的标准是,是否可以转换为数字,不能转换的就是 true,能转换的就是 false');
83             tail('推断:Number.isNaN 修复了上面的问题,只有 NaN会返回 true,其余情况一律返回 false');
84             function tail(text) {
85                 const TR = document.createElement('tr');
86                 const TD = document.createElement('td');
87                 TD.setAttribute('colspan', '4');
88                 TD.innerText = text;
89                 TR.appendChild(TD);
90                 TBODY.appendChild(TR);
91             }
92         </script>
93     </body>
94 </html>

 

标签:appendChild,isNaN,const,TR,Number,window,TD,document
From: https://www.cnblogs.com/aurora-power/p/17988819

相关文章

  • 在 Windows 中配置 WSL2 与 Debian 的全流程
    在Windows中配置WSL2与Debian的全流程ref:HowtoInstallWSL2onWindows10(Updated)-visitedon2024-01-09Microsoft-如何使用WSL在Windows上安装Linux-visitedon2024-01-09清华大学开源软件镜像站-Debian软件源-visitedon2024-01-09ArchWiki:F......
  • Installing Cassandra on Windows
    *[ApacheCassandra|ApacheCassandraDocumentation](https://cassandra.apache.org/_/download.html) *[InstallingCassandraonWindows](https://docs.nomagic.com/display/TWCloud2022x/Installing+Cassandra+on+Windows#:~:text=Cassandra%20is%20not%20supporte......
  • 记windows自定义bat脚本自启动
    自定义Windows启动脚本简化版在本指南中,我们将使用一个简化的批处理文件(.bat)来演示如何创建自定义的Windows启动脚本。以下是一个基本的模板,您只需根据需要在:begin部分添加您的代码。@echooffif"%1"=="h"gotobeginrem获取脚本路径set"scriptPath=%~dp0"rem......
  • 关于使用c++制作蓝牙连接,Windows版本
    1#define_CRT_SECURE_NO_WARNINGS2#pragmawarning(disable:4995)3#include<iostream>4#include<string>5#include<vector>6#include<sstream>7#include<iomanip>8#include<windows.h......
  • [原创]Windows安装配置PostgreSql_15.5.1数据库
    [原创]Windows安装配置PostgreSql_15.5.1数据库   PostgreSql数据库有多种安装方式,windows上常用的是installer方式、binary手动安装方式,本文采用手动安装的方式处理。总体过程比较简单,有mysql配置经验的分分钟的事儿。    一、下载并解压文件到具体安装目录。 ......
  • windows程序设计---使用c语言开发windows桌面应用程序
     消息机制--队列消息(常规消息鼠标,键盘等等,经过消息循环)GetMessage()得到消息--从消息队列中检索,DispatchMessage()分发消息消息机制----非对列消息-----调用特定windows程序函数触发的消息如:CreateWindow()函数被调用则发送WM_PAINT消息   windows窗口,非客户区是无......
  • VMware下Windows 7安装VMware Tools报错处理
    症状首先安装时它会弹窗选择始终安装最终安装中还是会弹窗提示...驱动安装失败,需要手动安装处理办法由于微软更新了驱动程序签名算法,2019年开始弃用SHA1,改用SHA2。猜测VMwareTools驱动程序使用SHA2,而Windows7只支持SHA1,需要下载安装补丁kb4474419来支持SHA2算法。......
  • 一种windows11连接蓝牙耳机后没有声音的特殊情况
    Windows11连接蓝牙耳机后,显示连接正常,且输出设备也已经切换至蓝牙耳机,可怎么都播放不出声音。最后,在设备管理器中禁用了”适用于蓝牙音频的英特尔智音技术“后,成功解决问题。供遇到类似情况的小伙伴们参考。......
  • 如何手工制作绿色免安装单文件同花顺免费版Windows客户端 2024-01-25
    如何手工制作绿色免安装单文件同花顺免费版Windows客户端  2024-01-25第1步、下载同花顺免费版http://download.10jqka.com.cn/第2步、安装同花顺免费版第3步、移动同花顺免费版软件到文件夹 D:\Prog\同花顺第4步、新建批处理脚本文件 D:\Prog\同花顺\一键打包\一键打......
  • jeecg-boot 同步数据库失败,Unable to perform unmarshalling at line number 5 and co
    同步数据库失败,Unabletoperformunmarshallingatlinenumber5andcolumn6.Message:cvc-complex-type.2.4.a:Invalidcontentwasfoundstartingwithelement'{"http://www.hibernate.org/xsd/orm/hbm":property}'.Oneof'{"http://www......