JavaScript/基本数据类型:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
无编辑摘要
 
(未显示同一用户的2个中间版本)
第1行: 第1行:
== 引言 ==
== 引言 ==
基本数据类型存储的是<code>值</code>,<code>值</code>不可调用。
基本数据类型存储的是<code>值</code>,<code>值</code>不可调用。
{{Leftrightflex|<syntaxhighlight lang="javascript">
{{Double-columns|<syntaxhighlight lang="javascript">
undefined.toString();</syntaxhighlight>
undefined.toString();</syntaxhighlight>
|{{Consoleout|<pre>Uncaught TypeError: Cannot read property 'toString' of undefined at <anonymous>:1:11
|{{Consoleout|<pre>Uncaught TypeError: Cannot read property 'toString' of undefined at <anonymous>:1:11
第8行: 第8行:
基础数据类型不具备对象属性{{qm|是不是叫对象属性}}。
基础数据类型不具备对象属性{{qm|是不是叫对象属性}}。


{{leftrightflex|<syntaxhighlight lang="javascript">
{{Double-columns|<syntaxhighlight lang="javascript">
var foo = 'hello';
var foo = 'hello';
foo = 'world';
foo = 'world';
第16行: 第16行:
'world'
'world'
undefined</pre>}}}}
undefined</pre>}}}}
== 精度 ==
[https://en.wikipedia.org/wiki/IEEE_754 IEEE 754] 规定64位:
* 符号位:1位
* 指数:11位
* 有效 精度:53位(显式存储52位)
[[文件:IEEE 754 Double Floating Point Format.svg.png|无|IEEE 754 Double Floating Point Format.svg]]
== 库 ==
* bignumber
== 检测 ==
<syntaxhighlight lang="javascript">
String(Number('1152921504735848759'))=='1152921504735848759</syntaxhighlight>


== 强制转换类型 ==
== 强制转换类型 ==
原始类型被强制转换成 <code>String</code> 类型:
原始类型被强制转换成 <code>String</code> 类型:


{{Leftrightflex|<syntaxhighlight lang="javascript">
{{Double-columns|<syntaxhighlight lang="javascript">
'1'.toString();</syntaxhighlight>
'1'.toString();</syntaxhighlight>
|{{Consoleout|<pre>"1"
|{{Consoleout|<pre>"1"
第28行: 第42行:
原始类型 <code>Number</code> 属于浮点数类型。
原始类型 <code>Number</code> 属于浮点数类型。


{{Leftrightflex|<syntaxhighlight lang="javascript">
{{Double-columns|<syntaxhighlight lang="javascript">
0.1 + 0.2
0.1 + 0.2
0.1 + 0.2 !== 0.3</syntaxhighlight>
0.1 + 0.2 !== 0.3</syntaxhighlight>
第34行: 第48行:
true
true
</pre>}}}}
</pre>}}}}
== 疑点 ==
有人传言<code>闭包中的基本数据类型变量不保存在栈内存中,而是保存在堆内存中</code>{{qm}},有待考证。

2026年11月26日 (四) 05:08的最新版本

引言

基本数据类型存储的是值,值不可调用。

undefined.toString();
Uncaught TypeError: Cannot read property 'toString' of undefined at <anonymous>:1:11

属性

基础数据类型不具备对象属性?(是不是叫对象属性) 。

var foo = 'hello';
foo = 'world';
console.log(foo);
foo.bar = 1234;
console.log(foo.bar);
'world'
undefined

精度

IEEE 754 规定64位:

  • 符号位:1位
  • 指数:11位
  • 有效 精度:53位(显式存储52位)
IEEE 754 Double Floating Point Format.svg
IEEE 754 Double Floating Point Format.svg

库

  • bignumber

检测

String(Number('1152921504735848759'))=='1152921504735848759

强制转换类型

原始类型被强制转换成 String 类型:

'1'.toString();
"1"

浮点数 BUG

原始类型 Number 属于浮点数类型。

0.1 + 0.2
0.1 + 0.2 !== 0.3
0.30000000000000004
true

疑点

有人传言闭包中的基本数据类型变量不保存在栈内存中,而是保存在堆内存中?(未确定) ,有待考证。