-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharticle-22987.htm
208 lines (196 loc) · 16.4 KB
/
article-22987.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<!DOCTYPE html>
<html xml:lang="zh-CN" lang="zh-CN">
<head>
<link rel="canonical" href="https://windowsv2ray.github.io/news/article-22987.htm" />
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>JavaScript-2-类型转换、null和undefined的注意点</title>
<meta name="description" content="数据类型:number、string、boolean、null、undefined、object 1. number ==> string +‘’ 、String(number) 、number" />
<link rel="icon" href="/assets/website/img/windowsv2ray/favicon.ico" type="image/x-icon"/>
<meta name="author" content="Windows V2ray分享订阅站">
<meta property="og:type" content="article" />
<meta property="og:url" content="https://windowsv2ray.github.io/news/article-22987.htm" />
<meta property="og:site_name" content="Windows V2ray分享订阅站" />
<meta property="og:title" content="JavaScript-2-类型转换、null和undefined的注意点" />
<meta property="og:image" content="https://windowsv2ray.github.io/uploads/20240604/5b87a6f5ce71e7182137e356b5bf19d6.webp" />
<meta property="og:release_date" content="2024-12-08T07:15:01" />
<meta property="og:updated_time" content="2024-12-08T07:15:01" />
<meta property="og:description" content="数据类型:number、string、boolean、null、undefined、object 1. number ==> string +‘’ 、String(number) 、number" />
<meta name="applicable-device" content="pc,mobile" />
<meta name="renderer" content="webkit" />
<meta name="force-rendering" content="webkit" />
<meta http-equiv="Cache-Control" content="no-transform" />
<meta name="robots" content="max-image-preview:large" />
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="JavaScript-2-类型转换、null和undefined的注意点">
<meta name="format-detection" content="telephone=no">
<link rel="dns-prefetch" href="https:/www.googletagmanager.com">
<link rel="dns-prefetch" href="https://www.googleadservices.com">
<link rel="dns-prefetch" href="https://www.google-analytics.com">
<link rel="dns-prefetch" href="https://pagead2.googlesyndication.com">
<link rel="dns-prefetch" href="https://cm.g.doubleclick.net">
<link rel="stylesheet" href="/assets/website/js/frontend/windowsv2ray/animate/animate.css">
<link rel="stylesheet" href="/assets/website/css/windowsv2ray/bootstrap.css">
<link rel="stylesheet" href="/assets/website/css/windowsv2ray/maicons.css">
<link rel="stylesheet" href="/assets/website/js/frontend/windowsv2ray/owl-carousel/css/owl.carousel.css">
<link rel="stylesheet" href="/assets/website/css/windowsv2ray/theme.css">
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-JN82W0GJX5"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-JN82W0GJX5');
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3332997411212854"
crossorigin="anonymous"></script>
</head>
<body data-page="detail">
<!-- Back to top button -->
<div class="back-to-top"></div>
<header>
<nav class="navbar navbar-expand-lg navbar-light navbar-float">
<div class="container">
<a href="/" class="navbar-brand">
<span>Windows V2ray</span>
</a>
<button class="navbar-toggler" data-toggle="collapse" data-target="#navbarContent" aria-controls="navbarContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse" id="navbarContent">
<ul class="navbar-nav ml-lg-4 pt-3 pt-lg-0">
<li class="nav-item">
<a href="/" class="nav-link">首页</a>
</li>
<li class="nav-item">
<a href="/free-nodes/" class="nav-link">免费节点</a>
</li>
<li class="nav-item">
<a href="/paid-subscribe/" class="nav-link">推荐机场</a>
</li>
<li class="nav-item">
<a href="/client.htm" class="nav-link">客户端</a>
</li>
<li class="nav-item">
<a href="/news/" class="nav-link">新闻资讯</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container mt-5">
<div class="page-banner">
<div class="row justify-content-center align-items-center h-100">
<div class="col-md-10">
<h1 class="text-center">JavaScript-2-类型转换、null和undefined的注意点</h1>
<nav aria-label="Breadcrumb">
<ul class="breadcrumb justify-content-center py-0 bg-transparent">
<li class="breadcrumb-item"><a href="/">首页</a></li>
<li class="breadcrumb-item"><a href="/news/">新闻资讯</a></li>
<li class="breadcrumb-item active">正文</li>
</ul>
</nav>
</div>
</div>
</div>
</div>
</header>
<main>
<div class="page-section">
<div class="container">
<div class="row">
<div class="col-md-9">
<input type="hidden" id="share-website-info" data-name="" data-url="">
<div id="content_views" class="markdown_views prism-atom-one-dark"> <p>数据类型:number、string、boolean、null、undefined、object</p> <h4><a id="1_number__string_1" rel="nofollow"></a>1. number ==> string</h4> <p>+‘’ 、String(number) 、number.toString()</p> <pre><code>var a=12; var b=13; console.log(a+''+b,typeof(a+''+b)) /* 1213 string */ console.log(a.toString()+b,typeof(a.toString()+b)); /* 1213 string */ console.log(String(a)+11,typeof (String(a)+11)); /* 1211 string */</code></pre> <h4><a id="2_string__number_11" rel="nofollow"></a>2. string ==> number</h4> <p>Number()、+string、-string、parseFloat(s) 保留全部<br /> ~~string、parseInt(s) 只保留整数</p> <pre><code>var s1='12.9'; console.log(parseInt(s1)+1); /* 13 */ console.log(parseFloat(s1)+1); /* 13.9 */ console.log(Number(s1)+1); /* 13.9 */ var s3='12.9'; console.log(+s3+1); /* 13.9 */ console.log(-s3+1); /* 11.9 */ console.log(~~s3); /* 12 */</code></pre> <p>Number() 和 parseFloat() 区别:</p> <pre><code>var s2='12.9abc'; console.log(parseFloat(s2)); /* 12.9 */ console.log(Number(s2)); /* NaN : not a number 非数字*/</code></pre> <p><strong>ps:不过NaN为number的一种,即typeof NaN为number</strong><br /> 注意NaN的特殊:</p> <pre><code>console.log(Number==Number); /* true */ console.log(null==null); /* true */ console.log(undefined==undefined); /* true */ console.log(NaN==NaN); /* false */ console.log(typeof NaN); /* number */</code></pre> <h4><a id="3_numberstring__boolean_44" rel="nofollow"></a>3. number、string ==> boolean</h4> <p>Boolean()、!!number/string</p> <pre><code>var a=0,b=1,s1='',s2='hello',s3=null,s4=undefined; console.log(Boolean(a)); /* false */ console.log(Boolean(b)); /* true */ console.log(Boolean(s1)); /* false */ console.log(Boolean(s2)); /* true */ console.log(Boolean(s3)); /* false */ console.log(Boolean(s4)); /* false */ console.log(!!b); /* true */ console.log(!!s4); /* false */</code></pre> <h4><a id="4_boolean_number_59" rel="nofollow"></a>4. boolean==> number</h4> <pre><code>console.log(false+1); /* 1 */ console.log(true+1); /* 2 */</code></pre> <h4><a id="5__65" rel="nofollow"></a>5. 注意</h4> <p>【有些想测试的还没测。。。感兴趣的可以测一下】</p> <pre><code>console.log(undefined); /* undefined */ console.log(null); /* null */ console.log(undefined==null); /* true */ console.log(undefined==false); /* false */ console.log(!!undefined==false); /* true */ console.log(NaN==NaN); /* false */</code></pre> <blockquote> <p>【解释为什么console.log(undefined<mark>null); /* true */】(有的说undefined</mark>null是因为null为object,而undefined为类型,是null一特殊存在,不过好像最初是这样用,现在摒弃了。。。但不确定这样说对不对。。。)<br /><strong>null</strong> 表示 “没有对象”,即该处不应该有值(典型用法:(1)作为函数的参数,表示该函数的参数不是对象;(2)作为对象原型链的终点。(这里不是很懂QAQ。。。))<br /><strong>undefined</strong>表示“缺少值“,即此处该有个值,但是没有定义(典型用法:(1)变量被声明了,但没有赋值时,就等于undefined;(2)调用函数时,应该提供的参数没有提供,该参数等于undefined;(3)对象没有赋值的属性,该属性的值为undefined;(4)函数没有返回值时,默认返回undefined)</p> </blockquote> </div> <div class="clearfix"></div>
<div class="col-md-12 mt-5">
<p>上一个:<a href="/news/article-22530.htm">宠物饲料营销方案(宠物饲料营销方案模板)</a></p>
<p>下一个:<a href="/news/article-22988.htm">瑞鹏宠物医院总店(瑞鹏宠物医院总店电话)</a></p>
</div>
</div>
<div class="col-md-3">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">热门文章</h3>
</div>
<div class="panel-body">
<ul class="p-0 x-0" style="list-style: none;margin: 0;padding: 0;">
<li class="py-2"><a href="/free-nodes/2025-3-2-free-v2ray-subscribe.htm" title="「3月2日」最高速度20.5M/S,2025年Clash/SSR/Shadowrocket/V2ray每天更新免费节点订阅链接">「3月2日」最高速度20.5M/S,2025年Clash/SSR/Shadowrocket/V2ray每天更新免费节点订阅链接</a></li>
<li class="py-2"><a href="/free-nodes/2025-2-16-node-share-links.htm" title="「2月16日」最高速度20.7M/S,2025年Shadowrocket/V2ray/Clash/SSR每天更新免费节点订阅链接">「2月16日」最高速度20.7M/S,2025年Shadowrocket/V2ray/Clash/SSR每天更新免费节点订阅链接</a></li>
<li class="py-2"><a href="/news/article-28085.htm" title="动物医院名字关键字是什么意思(动物医院的标志是什么)">动物医院名字关键字是什么意思(动物医院的标志是什么)</a></li>
<li class="py-2"><a href="/news/article-35950.htm" title="沈阳宠物领养基地(沈阳宠物犬领养)">沈阳宠物领养基地(沈阳宠物犬领养)</a></li>
<li class="py-2"><a href="/free-nodes/2024-12-16-free-ssr-subscribe.htm" title="「12月16日」最高速度20.2M/S,2024年SSR/Clash/V2ray/Shadowrocket每天更新免费节点订阅链接">「12月16日」最高速度20.2M/S,2024年SSR/Clash/V2ray/Shadowrocket每天更新免费节点订阅链接</a></li>
<li class="py-2"><a href="/free-nodes/2024-12-24-free-v2ray.htm" title="「12月24日」最高速度20.5M/S,2024年V2ray/SSR/Shadowrocket/Clash每天更新免费节点订阅链接">「12月24日」最高速度20.5M/S,2024年V2ray/SSR/Shadowrocket/Clash每天更新免费节点订阅链接</a></li>
<li class="py-2"><a href="/news/article-53746.htm" title="宠物店的猫哪里进货(宠物猫进货渠道)">宠物店的猫哪里进货(宠物猫进货渠道)</a></li>
<li class="py-2"><a href="/free-nodes/2025-1-7-free-ssr-subscribe.htm" title="「1月7日」最高速度19.4M/S,2025年Clash/Shadowrocket/V2ray/SSR每天更新免费节点订阅链接">「1月7日」最高速度19.4M/S,2025年Clash/Shadowrocket/V2ray/SSR每天更新免费节点订阅链接</a></li>
<li class="py-2"><a href="/news/article-34963.htm" title="宠物店领养协议书范本(宠物领养协议免费下载)">宠物店领养协议书范本(宠物领养协议免费下载)</a></li>
<li class="py-2"><a href="/news/article-43560.htm" title="上海宠物中心领养地址在哪(上海宠物领养中心免费领养狗狗)">上海宠物中心领养地址在哪(上海宠物领养中心免费领养狗狗)</a></li>
</ul>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">归纳</h3>
</div>
<div class="panel-body">
<ul class="p-0 x-0" style="list-style: none;margin: 0;padding: 0;">
<li class="py-2">
<h4><span class="badge" style="float: right;">6</span> <a href="/date/2025-03/" title="2025-03 归档">2025-03</a></h4>
</li>
<li class="py-2">
<h4><span class="badge" style="float: right;">84</span> <a href="/date/2025-02/" title="2025-02 归档">2025-02</a></h4>
</li>
<li class="py-2">
<h4><span class="badge" style="float: right;">92</span> <a href="/date/2025-01/" title="2025-01 归档">2025-01</a></h4>
</li>
<li class="py-2">
<h4><span class="badge" style="float: right;">87</span> <a href="/date/2024-12/" title="2024-12 归档">2024-12</a></h4>
</li>
</ul>
</div>
</div>
</div>
</div>
</div> <!-- .container -->
</div> <!-- .page-section -->
</main>
<footer class="page-footer">
<div class="container">
<div class="row">
<div class="col-sm-6 py-2">
<p id="copyright">
<p>
<a href="/">首页</a> |
<a href="/free-node/">免费节点</a> |
<a href="/news/">新闻资讯</a> |
<a href="/about-us.htm">关于我们</a> |
<a href="/disclaimer.htm">免责申明</a> |
<a href="/privacy.htm">隐私申明</a> |
<a href="/sitemap.xml">网站地图</a>
</p>
Windows V2ray分享订阅站 版权所有 Powered by WordPress
</p>
</div>
<div class="col-sm-6 py-2 text-right">
<div class="d-inline-block px-3">
<a href="#">Privacy</a>
</div>
<div class="d-inline-block px-3">
<a href="#">Contact Us</a>
</div>
</div>
</div>
</div> <!-- .container -->
</footer> <!-- .page-footer -->
<script src="/assets/website/js/frontend/windowsv2ray/jquery-3.5.1.min.js"></script>
<script src="/assets/website/js/frontend/windowsv2ray/bootstrap.bundle.min.js"></script>
<script src="/assets/website/js/frontend/windowsv2ray/wow/wow.min.js"></script>
<script src="/assets/website/js/frontend/windowsv2ray/owl-carousel/js/owl.carousel.min.js"></script>
<script src="/assets/website/js/frontend/windowsv2ray/waypoints/jquery.waypoints.min.js"></script>
<script src="/assets/website/js/frontend/windowsv2ray/animateNumber/jquery.animateNumber.min.js"></script>
<script src="/assets/website/js/frontend/windowsv2ray/theme.js"></script>
<script src="https://www.freeclashnode.com/assets/js/frontend/invite-url.js"></script>
<script src="/assets/website/js/frontend/G.js"></script>
</body>
</html>