Joe*_*ton 4 javascript compiler-construction computer-science
我似乎无法找到关于这两者之间的区别的明确解释.我还想指出,我也不太了解文字和价值观之间的区别.
布尔文字是否使用布尔对象?
dte*_*ech 14
一个文字是你的值从字面上提供脚本,所以他们是固定的.
甲值是"一段数据".因此,文字是一个值,但并非所有值都是文字.
例:
1; // 1 is a literal
var x = 2; // x takes the value of the literal 2
x = x + 3; // Adds the value of the literal 3 to x. x now has the value 5, but 5 is not a literal.
Run Code Online (Sandbox Code Playgroud)
对于问题的第二部分,您需要知道原语是什么.它比这复杂一点,但您可以将它们视为"所有不是对象的类型".Javascript有5个,包括boolean
和number
.所以那些通常不是一个对象.
为什么你还能(152).toString()
用Javascript 做什么?这是因为一种称为强制的机制(在其他语言中也称为自动装箱).需要时,Javascript引擎将在原语及其对象包装器之间进行转换,例如boolean
和Boolean
.这是对Javascript原语和自动装箱的一个很好的解释.
并不是说这种行为有时并不是你所期望的,尤其是 Boolean
例:
true; // this is a `boolean` primitive
new Boolean(true); // This results in an object, but the literal `true` is still a primitive
(true).toString(); // The literal true is converted into a Boolean object and its toString method is called
if(new Boolean(false)) { alert('Eh?'); }; // Will alert, as every Boolean object that isn't null or undefined evaluates to true (since it exists)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5320 次 |
最近记录: |