JS variable type is divided into
Value type (basic types): null undefined Number String Boolean
Reference types:
array object
In JS, each requires a data memory space. Memory space has been divided into two types of stack memory (stack) and heap memory (heap) .
Stack memory is typically stored basic data type
There are a = 1
Data stored in the stack memory use stack data structure similar to data structure, following the LIFO principle.
Usually heap memory storing reference data types
var b = {xi: 20}
Shallow copy
Shallow copy is a copy of only one
function shallowClone(source) {
var target = {};
for (var i in source) {
if (source.hasOwnProperty(i)) {
target[i] = source[i];
}
}
return target;
}
json deep copy
function cloneJSON(source) {
return JSON.parse(JSON.stringify(source));
}
= + Deep copy shallow copy recursively
Drawbacks: for regular objects, func, etc. do not deal with
function clone(source) {
var target = {};
for(var i in source) {
if (source.hasOwnProperty(i)) {
if (typeof source[i] === 'object') {
target [i] = clone (source [i]); // Note that
} else {
target[i] = source[i];
}
}
}
return target;
}
function cloneJSON(source) { returnJSON.parse(JSON.stringify(source)); }