1. Data type judgment
Stack: primitive data type (undefined, null, Boolean, Number, String)
heap: reference data type (object, function, array)
The difference between the two is that the storage location is different.
boolean typeof -> boolean
string typeof -> string
number typeof -> number
特殊值:Infinity正无穷大 / -Infinity负无穷大 / NaN
null typeof -> object null === null
undefined typeof -> undefined undefined === undefined
array typeof -> object
精确判断方法:xx instanceof Array 为true时
object typeof -> object
精确判断方法:xx instanceof Array 为false时
function typeof -> function
Judgment object example:
var obj = {
};
if(typeof obj === 'object' && !(obj instanceof Array)){
console.log("obj 是一个对象");
}
The definition and value of object and Array
-
Object: an unordered collection of key-value pairs
Array: an object whose property name is a number -
Traverse the object: for key in loop
Traverse the array: for... loop -
Object value: obj.key, obj[key]
----Assignment: obj.key = value, obj[key] = value
The difference between obj [key] and obj.key
- obj.key: Only normal values can be obtained
- obj[key]: You can get variables or complex strings
var obj = {
name: "yokia", age: 11 };
var myname = "name";
console.log(obj[myname]); // yokia
console.log(obj.myname); // undefined
Second, the object depth clone
Shallow cloning of objects
Variables whose reference type values point to the same heap address are congruent. If one variable is changed, the other variable will be changed accordingly.
method:
- Direct assignment
- for creates a new variable
- Object.assign(obj1, obj2 …) The merge of objects merges the properties of all objects starting from the second into the first object, and returns the merged object. If the attribute in the following object has the same name as the attribute in the preceding object, the value will be overwritten.
Shallow clone of array
The two arrays point to the same heap address, one is changed, and the other is changed.
method:
- Direct assignment
- for to create a new variable, push() in turn
- slice() cut from the beginning
- concat() connect with empty object
var arr1 = [];
for(var i = 0; i < arr.length; i ++){
arr1.push(arr[i]);
}
console.log("for arr", arr1 === arr, arr1); // true
var arr2 = arr.slice(0);
console.log("slice ", arr2 === arr, arr2); // true
var arr3 = [].concat(arr);
console.log("concat ", arr3 === arr, arr3); // true
console.log(arr[3] === arr1[3]); // true
Deep clone
- JSON.parse(JSON.stringify(obj)): The function function in the object will be lost
var user = {
name: "libai",
age: 12,
friend: ["汪伦"],
sayName: function(){
console.log(this.name);
}
}
var user2 = JSON.parse(JSON.stringify(user));
console.log("user2", user2);
- Encapsulate the deep clone function, using recursive thinking:
// 函数方法的深克隆
function deepclone(obj){
// 排除null
if(obj === null){
return null;
}
// 如果是数组类型,新对象也应该是数组类型
var newObj = obj instanceof Array ? [] : {
};
for(var i in obj){
newobj[i] =
typeof obj[i] === "object" ? deepclone(obj[i]) : obj[i];
// obj[i] 是一个引用类型的时候 null array object
// if(typeof obj[i] === "object"){
// newobj[i] = deepclone(obj[i]);
// }else{
// newobj[i] = obj[i];
// }
}
return newObj;
}