可能重复:
在JavaScript中使用toString
152..toString(2)
Run Code Online (Sandbox Code Playgroud)
正确创建二进制字符串"10011000",但是
152.toString(2)
Run Code Online (Sandbox Code Playgroud)
抛出一个例外
"SyntaxError:标识符在数字文字后立即启动"
为什么?后一种语法实际上听起来更正确,而前者看起来很奇怪!
我试图调用文字函数,但我得到了奇怪的行为.
考虑这个返回的代码true.
23 === (23)
Run Code Online (Sandbox Code Playgroud)
当我写作尝试以下.
(23).toFixed(2)
Run Code Online (Sandbox Code Playgroud)
我得到了预期的结果_23.00_但是当我尝试时23.toFixed(2)我得到了这个错误.
SyntaxError:意外的标记ILLEGAL
JavaScript如何评估无法理解的表达式以及为什么会出现此错误?
如果查看ECMAScript 3规范,您将看到原始值类型Null和Undefined没有伴随Null和Undefined对象.
>> Null
ReferenceError: Null is not defined
Run Code Online (Sandbox Code Playgroud)
其他原始值类型Number,String和Boolean类型都有伴随的Number,String和Boolean对象,您可以从全局范围引用它们.
>>Number
function Number() { [native code] }
>>Boolean
function Boolean() { [native code] }
Run Code Online (Sandbox Code Playgroud)
这些原始值类型的目的是提供诸如toString和valueOf它们各自的原始值类型的方法:
>>var n = 1;
>>n.toString();
"1"
Run Code Online (Sandbox Code Playgroud)
是相同的
>>var n = 1;
>>Number.prototype.toString.call(n);
"1"
Run Code Online (Sandbox Code Playgroud)
布尔值和字符串也是这样工作的:
>>var b = true;
>>b.toString();
"true"
>>Boolean.prototype.toString.call(b);
"true"
Run Code Online (Sandbox Code Playgroud)
当您尝试混合类型时,您可以看到原始值对象正在使用其伴随对象的方法:
>>Boolean.prototype.toString.call(n);
TypeError: Boolean.prototype.toString is not generic
>>Number.prototype.toString.call(b)
TypeError: Number.prototype.toString is not generic
Run Code Online (Sandbox Code Playgroud)
有趣的是,对于布尔和字符串文字类型,您可以直接从文字中调用这些方法:
>>true.toString();
"true"
>>Boolean.prototype.toString.call(true)
"true"
>>"moo".toString();
"moo"
>>String.prototype.toString.call("moo")
"moo"
Run Code Online (Sandbox Code Playgroud)
原始值为null和undefined,因为它们没有附带的Null和Undefined对象不能执行以下操作:
>>Null
ReferenceError: Null is not defined …Run Code Online (Sandbox Code Playgroud) 如果我试着写
3.toFixed(5)
Run Code Online (Sandbox Code Playgroud)
有一个语法错误.使用双点,放入空格,将三个放在括号中或使用括号表示法可以使其正常工作.
3..toFixed(5)
3 .toFixed(5)
(3).toFixed(5)
3["toFixed"](5)
Run Code Online (Sandbox Code Playgroud)
为什么单点符号不起作用,而我应该使用哪一种替代?
<script>
1..z
</script>
Run Code Online (Sandbox Code Playgroud)
这不会产生语法或运行时错误.看起来数字和变量名称可以是任何其他(123..qwerty).我想知道这句话是什么意思?
为什么2..toString()返回2但2.toString()会抛出此错误?
例:
console.log(2..toString()); // prints 2
// Firefox throws the error
// `SyntaxError: identifier starts immediately after numeric literal`
console.log(2.toString());
var x = 2;
console.log(x.toString()); // prints 2
// Firefox throws the error
//`TypeError: XML descendants internal method called on incompatible Number`
console.log(x..toString());
Run Code Online (Sandbox Code Playgroud) 为什么
123.toString()
Run Code Online (Sandbox Code Playgroud)
抛出一个SyntaxError
而
123..toString()
Run Code Online (Sandbox Code Playgroud)
不是?
当我做
1.toSting()
Run Code Online (Sandbox Code Playgroud)
我收到一个错误,但是
// javascript
var a = 1;
// or c#
int a = 1
a.toString()
Run Code Online (Sandbox Code Playgroud)
作品.为什么当一个数字被分配给一个变量时,它会得到一些特殊的功能呢?
Number.prototype.xx = function(){
alert("hi");
}
5.xx();
我正在尝试使用原型扩展Number.