当前位置:网站首页>JS data types
JS data types
2022-08-09 00:07:00 【Biao Biao_】
Basic data types: Number, String, Boolean, Null, Undefined, Symbol (ES6)
Reference data type: Object (In JS, all objects except basic data types are objects, and data is an object.Functions are objects, regular expressions are objects, etc.)
About Symbol: https://blog.csdn.net/weixin_34354945/article/details/91460921
The basic data type definition in JS is similar to the value type in C# (String is a reference type in C#), and so is the reference data type.
Continue reading, their storage methods are similar, the basic data type is stored on the stack, the reference data type is the specific value stored in the heap, and the reference address pointing to the specific value in the heap is stored on the stack.
The first sentence defines the object a, the secondThe sentence var c = a actually just copies the reference address of a and gives it to c. For the third sentence, after modifying the name of c to another value, the name of a has also changed, because the name attribute of c has been modified.When it is modified in the heap according to the reference address.When a accesses the object according to the same reference address, the object is naturally changed.
Here is another knowledge point: shallow copy and deep copy
Shallow copy:
Shallow copy will copy each attribute of the object in turn, but when the attribute value of the object is a reference type, it will be invalid, that is to say, onlyCopy one layer.
Object.assign is used here for shallow copy, similar toThe shallow copy method also has spread operator... , Array.prototype.slice(), Array.prototype.concat()
About the spread operator: http://www.fly63.com/article/detial/2516
Deep copy:
Deep copy copies variable values. For reference data, it recurses to basic types before copying.
The object after deep copy is completely isolated from the original object and does not affect each other. Modifications to one object will not affect the other object
let obj = {name: 'Michael',age: 18,others: {hobbies: ['basketball', 'gambling'],team: 'Bulls',},};let obj1 = deepClone(obj);obj1.age = 19;obj1.others.team = 'test';console.log('obj', obj);console.log('obj1', obj1);// recursively implement deep copyfunction deepClone(obj) {if (obj instanceof RegExp) return new RegExp(obj);if (obj instanceof Date) return new Date(obj);if (obj === null || typeof obj !== 'object') return obj //If it is not a reference type, return directlylet newObj = new obj.constructor(); //if obj is an array, newObj=[]; if obj is an object, newObj={}for (let key in obj) {if (obj.hasOwnProperty(key)) { //Is it its own objectnewObj[key] = deepClone(obj[key]) //assignment}}return newObj}

边栏推荐
猜你喜欢
随机推荐
JS基础-数组
Mysql Workbench导出sql文件出错:Error executing task: ‘ascii‘ codec can‘t decode byte 0xd0 in position 26:
第四章 SQL与关系数据库基本操作(下)
关于如何快速筛选素数的问题
ADXL345调试心得
snmp获取agent OID,及MibBrowser使用
GRPC学习(An RPC library and framework)
Anaconda 使用 Navigator 安装 Tensorflow(包括 Anaconda 安装)
2017年10月历史文章汇总
记一次“粗暴”的Flash模拟EEPROM法(用的STM32F030C6芯片,没找到模拟EEPROM库函数)
vs2012快捷键
Why software development methodology make you feel bad?
第二章 关系数据库概述
常用的正则表达式(不定期更新)
VsCode配置自己喜欢的字体,背景,妈妈再也不担心我写代码枯燥了
pycharm的远程运行环境设置
数据机构-哈夫曼树
nlp 评论分类实现总结
JS基础知识
穿越派如何续购相关产品功能








