JavaScript括号将原始类型转换为对象 [英] JavaScript parentheses converts primitive type to object

查看:96
本文介绍了JavaScript括号将原始类型转换为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果数字是原始类型,为什么我可以这样做:

If numbers are primitive types, why I can do:

> (12345).toString()
"12345"

括号转换基元类型到数字

推荐答案

不,括号只是让解析器理解不是小数点。

No, the parentheses are just letting the parser understand that the . is not a decimal point.

12345 .toString() 也可以。

每当您访问其属性时,原始数字会隐式转换为Numbers,但这些对象是临时的并且会立即丢失。例如:

Primitive numbers implicitly converted to Numbers whenever you access their properties, but the objects are temporary and immediately lost. For example:

var foo = 5;

foo.bar = "something";

console.log(foo.bar); // undefined

字符串和布尔值相同。

这篇关于JavaScript括号将原始类型转换为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆