当前位置:网站首页>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}
边栏推荐
猜你喜欢
随机推荐
如何购买穿越派V3.14版本产品?
oracle利用as of timestamp语句找回误删除的数据
Mysql Workbench用.sql文件将数据导入数据库
js高级进阶知识
IDEA常用插件及代快捷键模板整理
轮流取石头游戏
HCIP2---静态路由、动态路由
Why software development methodology make you feel bad?
如何使用加密套件?
WeChat applet console error - summary 】 【
Laravel框架之文件上传
LeetCode 0179. 最大数
C--《C和指针》第七章读书笔记
MVC和MVVM
穿越派(v3.14)版本可以试用啦!
牛客练习赛88 补题
C——《C和指针》第六章读书笔记
第二章 关系数据库概述
The difference between MVC and MVP
C#中的&、&&、|、||的区别