一.JS中的数据类型
1.基本数据类型:Number,String,Boolean,Undefined,Null,Symbol
2.复杂数据类型:Object,Array,Function,Map,Set
二.JS中判断数据类型
1.typeof
可以判断Number,String,Boolean,Undefined,Function,Symbol但是Null,Object,Array,Map,Set这三种类型都会判断为Object
2.instanceof
obj instanceof Object:判断Object是否在obj的调用链上
obj instanceof Object;// true arr instanceof Array;// true map instanceof Map //true set instanceof Set //true null instanceof Object;// false undefined instanceof Object;// false symbol instanceof Symbol //false
instanceof不能区别undefined和null,而且对于基本类型如果不是用new声明的则也测试不出来,对于是使用new声明的类型,它还可以检测出多层继承关系
3.object.prototype.toString
-
console.
log(
Object.
prototype.
toString.
call(bool));
//[object Boolean]
-
console.
log(
Object.
prototype.
toString.
call(num));
//[object Number]
-
console.
log(
Object.
prototype.
toString.
call(str));
//[object String]
-
console.
log(
Object.
prototype.
toString.
call(und));
//[object Undefined]
-
console.
log(
Object.
prototype.
toString.
call(nul));
//[object Null]
-
console.
log(
Object.
prototype.
toString.
call(arr));
//[object Array]
-
console.
log(
Object.
prototype.
toString.
call(obj));
//[object Object]
-
console.
log(
Object.
prototype.
toString.
call(fun));
//[object Function]
-
console.
log(
Object.
prototype.
toString.
call(s1));
//[object Symbol]
-
console.
log(
Object.
prototype.
toString.
call(map));
//[object Map]
-
console.
log(
Object.
prototype.
toString.
call(set));
//[object Set]
-
-
function
Person(
){}
-
function
Student(
){}
-
Student.
prototype =
new
Person()
-
var haoxl =
new
Student()
-
console.
log(
Object.
prototype.
toString.
call(haoxl));
//[object Object]
三.深拷贝
1.常见的浅拷贝
1.1 Object.assign
将obj1和obj2中的可枚举属性拷贝到第一个目标对象中,并且将这个对象返回
let obj = Object.assign({},obj1,obj2);
1.2 Array.prototype.concat()
将alpha和numeric合并为一个新数组并返回
-
var alpha = [
'a',
'b',
'c'];
-
var numeric = [
1,
2,
3];
-
-
alpha.
concat(numeric);
-
// result in ['a', 'b', 'c', 1, 2, 3]
1.3 Array.prototype.slice()
slice()
方法返回一个新的数组对象,这一对象是一个由 begin
和 end
决定的原数组的浅拷贝(包括 begin
,不包括end
)。原始数组不会被改变。
-
const animals = [
'ant',
'bison',
'camel',
'duck',
'elephant'];
-
-
console.
log(animals.
slice(
2));
-
// expected output: Array ["camel", "duck", "elephant"]
-
-
console.
log(animals.
slice(
2,
4));
-
// expected output: Array ["camel", "duck"]
1.4 扩展运算符 ...
2.简单实现深拷贝
JSON.parse(JSON.stringify(obj))
- 可以处理普通值(string,number,boolean),对象和数组
- 不能处理函数,map,set,symbol类型的值
- 并且当对象中包含循环引用时会报错(obj.info = obj)
3.完整的深拷贝
封装一个函数来实现:
-
function
isObject(
value) {
-
const valueType =
typeof value
-
return (value !==
null) && (valueType ===
"object")
-
}
-
-
-
function
deepClone(
originValue, map = new WeakMap()) {
-
// 判断是否是一个Set类型
-
if (originValue
instanceof
Set) {
-
return
new
Set([...originValue])
-
}
-
-
// 判断是否是一个Map类型
-
if (originValue
instanceof
Map) {
-
return
new
Map([...originValue])
-
}
-
-
// 判断如果是Symbol的value, 那么创建一个新的Symbol
-
if (
typeof originValue ===
"symbol") {
-
return
Symbol(originValue.
description)
-
}
-
-
// 判断如果是函数类型, 那么直接使用同一个函数
-
if (
typeof originValue ===
"function") {
-
return originValue
-
}
-
-
// 判断传入的originValue是否是一个对象类型
-
if (!
isObject(originValue)) {
-
return originValue
-
}
-
if (map.
has(originValue)) {
-
return map.
get(originValue)
-
}
-
-
// 判断传入的对象是数组, 还是对象
-
const newObject =
Array.
isArray(originValue) ? []: {}
-
map.
set(originValue, newObject)
-
for (
const key
in originValue) {
-
newObject[key] =
deepClone(originValue[key], map)
-
}
-
-
// 对Symbol的key进行特殊的处理
-
const symbolKeys =
Object.
getOwnPropertySymbols(originValue)
-
for (
const sKey
of symbolKeys) {
-
// const newSKey = Symbol(sKey.description)
-
newObject[sKey] =
deepClone(originValue[sKey], map)
-
}
-
-
return newObject
-
}
转载:https://blog.csdn.net/ICanWin_lll/article/details/125514434
查看评论