当前位置:网站首页>JS的装箱和拆箱
JS的装箱和拆箱
2022-07-03 02:19:00 【weixin_49035434】
装箱转换
每一种基本类型Number、String、Boolean、Symbol在对象中都有对应的类,所谓装箱转换,正是把基本类型转换为对应的对象,它是类型转换中一种相当重要的种类。
前文提到,全局的 Symbol 函数无法使用 new 来调用,但我们仍可以利用装箱机制来得到一个 Symbol 对象,我们可以利用一个函数的call方法来强迫产生装箱。
我们定义一个函数,函数里面只有return this,然后我们调用函数的call方法到一个Symbol类型的值上,这样就会产生一个symbolObject。
我们可以用console.log看一下这个东西的type of,它的值是object,我们使用symbolObject instanceof 可以看到,它是Symbol这个类的实例,我们找它的constructor也是等于Symbol的,所以我们无论从哪个角度看,它都是Symbol装箱过的对象:
var symbolObject = (function(){ return this; }).call(Symbol("a"));
console.log(typeof symbolObject); //object
console.log(symbolObject instanceof Symbol); //true
console.log(symbolObject.constructor == Symbol); //true
装箱机制会频繁产生临时对象,在一些对性能要求较高的场景下,我们应该尽量避免对基本类型做装箱转换。
使用内置的 Object 函数,我们可以在JavaScript代码中显式调用装箱能力。
var symbolObject = Object(Symbol("a"));
console.log(typeof symbolObject); //object
console.log(symbolObject instanceof Symbol); //true
console.log(symbolObject.constructor == Symbol); //true
每一类装箱对象皆有私有的 Class 属性,这些属性可以用 Object.prototype.toString 获取:
var symbolObject = Object(Symbol("a"));
console.log(Object.prototype.toString.call(symbolObject)); //[object Symbol]
在 JavaScript 中,没有任何方法可以更改私有的 Class 属性,因此Object.prototype.toString 是可以准确识别对象对应的基本类型的方法,它比 instanceof 更加准确。
但需要注意的是,call本身会产生装箱操作,所以需要配合 typeof 来区分基本类型还是对象类型。
拆箱转换
在JavaScript标准中,规定了 ToPrimitive 函数,它是对象类型到基本类型的转换(即,拆箱转换)。
对象到 String 和 Number 的转换都遵循“先拆箱再转换”的规则。通过拆箱转换,把对象变成基本类型,再从基本类型转换为对应的 String 或者 Number。
拆箱转换会尝试调用 valueOf 和 toString 来获得拆箱后的基本类型。如果 valueOf 和 toString 都不存在,或者没有返回基本类型,则会产生类型错误 TypeError。
var o = {
valueOf : () => {console.log("valueOf"); return {}},
toString : () => {console.log("toString"); return {}}
}
o * 2
// valueOf
// toString
// TypeError
我们定义了一个对象o,o有valueOf和toString两个方法,这两个方法都返回一个对象,然后我们进行o*2这个运算的时候,你会看见先执行了valueOf,接下来是toString,最后抛出了一个TypeError,这就说明了这个拆箱转换失败了。
到 String 的拆箱转换会优先调用 toString。我们把刚才的运算从o*2换成 String(o),那么你会看到调用顺序就变了。
var o = {
valueOf : () => {console.log("valueOf"); return {}},
toString : () => {console.log("toString"); return {}}
}
String(o)
// toString
// valueOf
// TypeError
在 ES6 之后,还允许对象通过显式指定 @@toPrimitive Symbol 来覆盖原有的行为。
var o = {
valueOf : () => {console.log("valueOf"); return {}},
toString : () => {console.log("toString"); return {}}
}
o[Symbol.toPrimitive] = () => {console.log("toPrimitive"); return "hello"}
console.log(o + "")
// toPrimitive
// hello边栏推荐
- 人脸识别6- face_recognition_py-基于OpenCV使用Haar级联与dlib库进行人脸检测及实时跟踪
- [shutter] shutter debugging (debugging control related functions | breakpoint management | code operation control)
- 通达OA v12流程中心
- Unrecognized SSL message, plaintext connection?
- Face recognition 6-face_ recognition_ Py based on OpenCV, face detection and real-time tracking using Haar cascade and Dlib Library
- 单词单词单词
- GBase 8c系统表-pg_conversion
- 502 (bad gateway) causes and Solutions
- Tongda OA homepage portal workbench
- 创建+注册 子应用_定义路由,全局路由与子路由
猜你喜欢

Flink CDC mongoDB 使用及Flink sql解析monggo中复杂嵌套JSON数据实现

SPI mechanism

基于线程池的生产者消费者模型(含阻塞队列)
![[shutter] bottom navigation bar implementation (bottomnavigationbar bottom navigation bar | bottomnavigationbaritem navigation bar entry | pageview)](/img/41/2413af283e8f1db5d20ea845527175.gif)
[shutter] bottom navigation bar implementation (bottomnavigationbar bottom navigation bar | bottomnavigationbaritem navigation bar entry | pageview)

stm32F407-------DMA

Solution for processing overtime orders (Overtime unpaid)

How do it students find short-term internships? Which is better, short-term internship or long-term internship?

y54.第三章 Kubernetes从入门到精通 -- ingress(二七)

通达OA v12流程中心

Tongda OA homepage portal workbench
随机推荐
[shutter] shutter debugging (debugging control related functions | breakpoint management | code operation control)
Basic operation of view
GBase 8c系统表pg_database
awk从入门到入土(0)awk概述
GBase 8c系统表-pg_authid
Unrecognized SSL message, plaintext connection?
easyExcel
Ni visa fails after LabVIEW installs the third-party visa software
Thread safe singleton mode
Hard core observation 547 large neural network may be beginning to become aware?
Prohibited package name
leetcode961. Find the elements repeated N times in the array with length 2n
Awk from entry to burial (1) awk first meeting
Stm32f407 ------- IIC communication protocol
COM and cn
GBase 8c触发器(三)
GBase 8c 创建用户/角色 示例二
返回一个树形结构数据
Kotlin middle process understanding and Practice (I)
awk从入门到入土(3)awk内置函数printf和print实现格式化打印