美文网首页
js实现深拷贝

js实现深拷贝

作者: 辰漪 | 来源:发表于2021-05-21 21:05 被阅读0次

一、js数据类型

基本数据类型:String、Number、Boolean、Null、Undefined
复杂数据类型:Object

二、js数据类型判断

let str = "字符串类型" 
let bool = true   
let num = 123 
let nulll = null  
let undef   
let arr = []   
let obj = {}   
let sum = function () {}  

1. 使用typeof进行判断

console.log(typeof str) // string
console.log(typeof bool) // boolean
console.log(typeof num) // number
console.log(typeof nulll) // object
console.log(typeof undef) // undefined
console.log(typeof arr) // object
console.log(typeof obj) // object
console.log(typeof sum) // function

使用typeof进行判断数据类型,只能够判断基本数据类型string number boolean 以及 function,而null和object不能够进一步的判断。

2. 使用A instanceof B进行判断

console.log(str instanceof String) // false
console.log(bool instanceof Boolean) // false
console.log(num instanceof Number) // false
console.log(nulll instanceof Object) // false
console.log(undef instanceof Object) // false
console.log(arr instanceof Array) // true
console.log(obj instanceof Object) // true
console.log(sum instanceof Function) // true

使用A instanceof B的方式进行判断,字面意思,A是否是B的实例,可以判断出Array和Object类型,但是undefined和null不能区分数据类型,基础的数据类型,因为不是使用new出来的,也测试不出来。

3. 使用Object.prototype.toString.call()进行判断

console.log(Object.prototype.toString.call(str)) // [object String]
console.log(Object.prototype.toString.call(bool)) // [object Boolean]
console.log(Object.prototype.toString.call(num)) // [object Number]
console.log(Object.prototype.toString.call(nulll)) // [object Null]
console.log(Object.prototype.toString.call(undef)) // [object Undefined]
console.log(Object.prototype.toString.call(arr)) // [object Array]
console.log(Object.prototype.toString.call(obj)) // [object Object]
console.log(Object.prototype.toString.call(sum)) // [object Function]

Object.prototype.toString()方法可以返回一个表示该对象的字符串'[object type]',为了每个对象都能通过 Object.prototype.toString() 来检测,需要以 Function.prototype.call() 或者 Function.prototype.apply() 的形式来调用,传递要检查的对象作为第一个参数,称为 thisArg。

三、实现深拷贝函数

function deepClone(ob) {
    if (typeof ob === "object") {
        if (Object.prototype.toString.call(ob).slice(8, -1) === 'Null') return ob
        if (ob instanceof Array) {
            // 数组
            let newArr = []
            ob.forEach((item, index, arr) => {
                newArr[index] = deepClone(item)
            })
            return newArr
        } else {
            // 对象
            let newObj = {}
            // for (let k in ob) {
            //  newObj[k] = deepClone(ob[k])
            // }
            Object.keys(ob).forEach((key, index, arr) => {
                newObj[key] = deepClone(ob[key])
            })
            return newObj
        }
    } else {
        return ob
    }
}

实现思路:

  1. 先判断是不是对象类型的,不是对象类型,直接返回出去
  2. 如果是对象类型的话,再判断是对象还是数组
  3. 对对象或者数组进行遍历,对值进行递归

其实还有很多深拷贝的方式,比如说Object.assign(),JSON.stringify()等方式,可以自己下去尝试一下哦!

相关文章

  • js浅拷贝深拷贝

    js浅拷贝,深拷贝的简单实现 基础数据 浅拷贝 深拷贝

  • JS 中深拷贝的几种实现方法

    JS 中深拷贝的几种实现方法 1、使用递归的方式实现深拷贝 方法二 2、通过 JSON 对象实现深拷贝 缺点 (1...

  • Javascript中的深拷贝

    JS 中深拷贝的几种实现方法 1、使用递归的方式实现深拷贝 //使用递归的方式实现数组、对象的深拷贝 functi...

  • JS深拷贝的实现

    JS深拷贝, 即传值拷贝, 需要新建一处内存,实现拷贝,具体实现如下: //ES5深拷贝 (1)循环递归: fun...

  • js 实现深拷贝

    一般不会深拷贝函数等,所以一般用递归和JSON方法即可。 如果要实现函数等拷贝,比较完美的做法: function...

  • js实现深拷贝

    一、js数据类型 基本数据类型:String、Number、Boolean、Null、Undefined复杂数据类...

  • 手撕常见面试题

    DOM 事件代理 数组 对象 扁平化 去重 - unique() 拷贝 浅拷贝 深拷贝 copy()函数实现 JS...

  • JS中的深拷贝与浅拷贝

    知乎:js中的深拷贝和浅拷贝? 掘金: js 深拷贝 vs 浅拷贝 前言 首先深拷贝与浅拷贝只针对 Object,...

  • 2020前端高频面试题总结(附答案)

    [ js基础题 ] 1. new的实现原理是什么? 2. 深拷贝和浅拷贝的区别是什么 深拷贝 浅拷贝 3. bin...

  • iOS基础知识点(网络摘抄)

    1.父类实现深拷贝时,子类如何实现深拷贝。父类没有实现深拷贝时,子类如何实现深拷贝? 深拷贝同浅拷贝的区别:...

网友评论

      本文标题:js实现深拷贝

      本文链接:https://www.haomeiwen.com/subject/zcawjltx.html