JavaScript/Null:修订间差异
来自康健生活
无编辑摘要 |
|||
| (未显示同一用户的15个中间版本) | |||
| 第1行: | 第1行: | ||
== 引言 == | |||
在计算机科学中,null函数(或null运算符)是使程序状态保持不变的子例程。当它是处理器指令集的一部分时,称为NOP或NOOP(无操作)。 | |||
: <math>f(s)=s</math> for all elements <math>s \in S</math>. | |||
{| class="wikitable" | |||
|- | |||
! variable name | |||
| colspan=8 | null | |||
|- | |||
! value | |||
| colspan=8 | [null string] | |||
|- | |||
! hex value | |||
| 00 || 00 || 00 || 00 || 00 || 00 || 00 || 00 | |||
|} | |||
NULL并非总是和0等同,NULL仅仅代表空值,也就是指向一个不被使用的地址 | |||
NULL 是个没有值的值(NULL is a value that is not a value),表示“一个空对象指针”、“没有对象”、“空值”等概念,是一种特殊的对象值。 | |||
NULL 在其他语言中的变体,包括但不限于:<code>NULL</code>, <code>nil</code>, <code>None</code>, <code>Nothing</code>, <code>Nil</code>, <code>nullptr</code>等(C/C++:NULL, Lua: nil, python:None, VB:Nothing, ObjectC:Nil, C++11: nullptr)。 | NULL 在其他语言中的变体,包括但不限于:<code>NULL</code>, <code>nil</code>, <code>None</code>, <code>Nothing</code>, <code>Nil</code>, <code>nullptr</code>等(C/C++:NULL, Lua: nil, python:None, VB:Nothing, ObjectC:Nil, C++11: nullptr)。 | ||
| 第9行: | 第26行: | ||
在 JavaScript 设计之初,使用的是 32 位操作系统,为了性能考虑使用低位存储{{qm}}变量的类型信息,其中 <code>000</code> 开头代表对象,恰好 <code>Null</code> 转换成机器语言时是很多 | 在 JavaScript 设计之初,使用的是 32 位操作系统,为了性能考虑使用低位存储{{qm}}变量的类型信息,其中 <code>000</code> 开头代表对象,恰好 <code>Null</code> 转换成机器语言时是很多 | ||
Null 只有在对象引用(对象引用可以是变量,常量,属性(类字段),输入/输出参数等)具有“无关联值”的情况下才应允许使用。 | |||
Null 被认为是对象的占位符,以此解释 Null 的 typeof 返回为 Object 的历史问题。 | |||
== 判断为null == | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
Object.prototype.toString.call(null) === '[object Null]'</syntaxhighlight>|{{consoleout|<pre>true | |||
</pre>}}}} | |||
== Null 与 0 与 Undefined == | |||
== 强制类型转换 == | |||
原始类型被强制转换成 <code>Number</code> 类型: | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
Number(null);</syntaxhighlight> | |||
|{{Consoleout|<pre>0 | |||
</pre>}}}} | |||
== 缺点 == | == 缺点 == | ||
* 破坏类型(subverts types) | * 破坏类型(subverts types) | ||
*: 跳过了类型检测,直到程序运行抛出 <code>NULL</code> 引用错误。 | *: 跳过了类型检测,直到程序运行抛出 <code>NULL</code> 引用错误。 | ||
* 草率的(is sloppy) | * 草率的(is sloppy) | ||
*: 只要语言允许任意对象可以是 <code>NULL</code>。 | *: 只要语言允许任意对象可以是 <code>NULL</code>。<code>NULL</code> 与空字符容易混为一谈。 | ||
*: <syntaxhighlight lang="java">if(str==null || str.equals("")){}</syntaxhighlight> | *: <syntaxhighlight lang="java">if(str==null || str.equals("")){}</syntaxhighlight> | ||
*: <syntaxhighlight lang="c#">if(string.IsNullOrEmpty(str)){}</syntaxhighlight> | *: <syntaxhighlight lang="c#">if(string.IsNullOrEmpty(str)){}</syntaxhighlight> | ||
* 特例(is a special case) | * 特例(is a special case) | ||
*: <code>NULL</code> 是一个没有值的值,在许多情况下 <code>NULL</code> 变成了一个需要特别处理的地方。 | *: <code>NULL</code> 是一个没有值的值,在许多情况下 <code>NULL</code> 变成了一个需要特别处理的地方。 | ||
*: <syntaxhighlight lang="c#"> | |||
char c = 'A'; | char c = 'A'; | ||
char *myChar = &c; | char *myChar = &c; | ||
std::cout<<*myChar<<std::endl; | std::cout<<*myChar<<std::endl; | ||
char *myChar = 123; | char *myChar = 123; | ||
std::cout<<*myChar<<std::endl; | std::cout<<*myChar<<std::endl; // ==> 编译错误 | ||
char *myChar = 0; | char *myChar = 0; | ||
std::cout << *myChar << std::endl; | std::cout << *myChar << std::endl; // ==> 运行错误</syntaxhighlight> | ||
</syntaxhighlight | |||
* 使API捉襟见肘(makes poor APIs) | * 使API捉襟见肘(makes poor APIs) | ||
* 加剧了不好的编程策略(exacerbates poor language decisions) | * 加剧了不好的编程策略(exacerbates poor language decisions) | ||
* 难以调试(is difficult to debug) | * 难以调试(is difficult to debug) | ||
* 不可组合的(is non-composable) | * 不可组合的(is non-composable) | ||
2026年11月26日 (四) 05:06的最新版本
引言
在计算机科学中,null函数(或null运算符)是使程序状态保持不变的子例程。当它是处理器指令集的一部分时,称为NOP或NOOP(无操作)。
- for all elements .
| variable name | null | |||||||
|---|---|---|---|---|---|---|---|---|
| value | [null string] | |||||||
| hex value | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
NULL并非总是和0等同,NULL仅仅代表空值,也就是指向一个不被使用的地址
NULL 是个没有值的值(NULL is a value that is not a value),表示“一个空对象指针”、“没有对象”、“空值”等概念,是一种特殊的对象值。
NULL 在其他语言中的变体,包括但不限于:NULL, nil, None, Nothing, Nil, nullptr等(C/C++:NULL, Lua: nil, python:None, VB:Nothing, ObjectC:Nil, C++11: nullptr)。
Null 使用 typeof 判断时结果是 object,因为 Null 被转换为机器语言时是很多个 0,而只要前三个为零的话,就会被机器语言判断为 object ?(未确定) 。
在 JavaScript 设计之初,使用的是 32 位操作系统,为了性能考虑使用低位存储?(未确定) 变量的类型信息,其中 000 开头代表对象,恰好 Null 转换成机器语言时是很多
Null 只有在对象引用(对象引用可以是变量,常量,属性(类字段),输入/输出参数等)具有“无关联值”的情况下才应允许使用。
Null 被认为是对象的占位符,以此解释 Null 的 typeof 返回为 Object 的历史问题。
判断为null
Object.prototype.toString.call(null) === '[object Null]'
true
Null 与 0 与 Undefined
强制类型转换
原始类型被强制转换成 Number 类型:
Number(null);
0
缺点
- 破坏类型(subverts types)
- 跳过了类型检测,直到程序运行抛出
NULL引用错误。
- 跳过了类型检测,直到程序运行抛出
- 草率的(is sloppy)
- 只要语言允许任意对象可以是
NULL。NULL与空字符容易混为一谈。 if(str==null || str.equals("")){}
if(string.IsNullOrEmpty(str)){}
- 只要语言允许任意对象可以是
- 特例(is a special case)
NULL是一个没有值的值,在许多情况下NULL变成了一个需要特别处理的地方。char c = 'A'; char *myChar = &c; std::cout<<*myChar<<std::endl; char *myChar = 123; std::cout<<*myChar<<std::endl; // ==> 编译错误 char *myChar = 0; std::cout << *myChar << std::endl; // ==> 运行错误
- 使API捉襟见肘(makes poor APIs)
- 加剧了不好的编程策略(exacerbates poor language decisions)
- 难以调试(is difficult to debug)
- 不可组合的(is non-composable)
No categories