一蝙昙、數(shù)據(jù)類型
1葬项、六種基本數(shù)據(jù)類型
- undefined
- null
- string
- number(注意:NaN)
- boolean
- symbol(ES6)
2泞当、一種引用類型
- Object(包括Array和Function)
3、檢測方式
1)typeof(...)
用來檢測:undefined民珍、string零蓉、number笤受、boolean、symbol敌蜂、object箩兽、function
無法檢測引用類型里的Array
2)xx instanceof type
用來檢測引用類型是Array/Function/Object
無法檢測基本類型
console.log(2 instanceof Number); // false
console.log(true instanceof Boolean); // false
console.log('str' instanceof String); // false
console.log([] instanceof Array); // true
console.log(function(){} instanceof Function); // true
console.log({} instanceof Object); // true
console.log(undefined instanceof Undefined);// 報(bào)錯
console.log(null instanceof Null);//報(bào)錯
3)constructor
console.log((2).constructor === Number); //true
console.log((true).constructor === Boolean); //true
console.log(('str').constructor === String); //true
console.log(([]).constructor === Array); //true
console.log((function() {}).constructor === Function); //true
console.log(({}).constructor === Object); //true
但是如果創(chuàng)建的對象更改了原型,是無法檢測到最初的類型
function Fn(){}; //原來是方法
Fn.prototype=new Array(); //改變原型為數(shù)組
var f=new Fn();
console.log(f.constructor===Fn); // false
console.log(f.constructor===Array); // true
4)其他補(bǔ)充方法
- null檢測方式:a === null
- Array檢測方式:Array.isArray([...])
5)萬金油方法:Object.prototype.toString.call()
能檢測所有類型章喉,返回 "[object type]", 其中type是對象類型
var a = Object.prototype.toString;
console.log(a.call(2));
console.log(a.call(true));
console.log(a.call('str'));
console.log(a.call([]));
console.log(a.call(function(){}));
console.log(a.call({}));
console.log(a.call(undefined));
console.log(a.call(null));
[object Number]
[object Boolean]
[object String]
[object Undefined]
[object Null]
[object Array]
[object Function]
[object Object]
4汗贫、null和undefined區(qū)別
- 基本沒區(qū)別,都表示“無”
- 細(xì)微區(qū)別:
null表示"沒有對象"秸脱,即該處不應(yīng)該有值落包。典型用法是:
(1) 作為函數(shù)的參數(shù),表示該函數(shù)的參數(shù)不是對象摊唇。
(2) 作為對象原型鏈的終點(diǎn)咐蝇。
undefined表示"缺少值",就是此處應(yīng)該有一個值巷查,但是還沒有定義有序。典型用法是:
(1)變量被聲明了,但沒有賦值時岛请,就等于undefined旭寿。
(2) 調(diào)用函數(shù)時,應(yīng)該提供的參數(shù)沒有提供崇败,該參數(shù)等于undefined盅称。
(3)對象沒有賦值的屬性,該屬性的值為undefined后室。
(4)函數(shù)沒有返回值時缩膝,默認(rèn)返回undefined。
二岸霹、棧和堆
1疾层、定義
- 棧stack為自動分配的內(nèi)存空間,它由系統(tǒng)自動釋放松申;
- 堆heap則是動態(tài)分配的內(nèi)存云芦,大小不定也不會自動釋放。
?2贸桶、與數(shù)據(jù)類型的關(guān)系
基本數(shù)據(jù)類型存放在棧里舅逸,=:直接傳值
-
引用數(shù)據(jù)類型存放在堆里,=:傳址
image
題目:
三皇筛、淺/深拷貝
1琉历、基本概念
前奏:淺/深拷貝主要針對引用類型
因?yàn)閷τ诨緮?shù)據(jù)類型而言:
var a = 2
var b = a
a = 1
console.log(a, b) // 1 ,2 ——a,b指向棧里不同數(shù)據(jù)
而對于引用類型而言:
var a = {c: 2}
var b = a
a.c = 1
console.log(a.c,b.c) //1,1 —— a,b指向堆里同份數(shù)據(jù)
為了切斷引用類型a和b的聯(lián)系,所以我們需要淺/深拷貝旗笔,
- 淺拷貝:一層拷貝
- 深拷貝:無限層拷貝
2彪置、兩大類:數(shù)組/對象
數(shù)組的淺拷貝:數(shù)組里的引用類型都是淺拷貝的
/**
數(shù)組的淺拷貝
**/
//1、基本 =
var arr1 = [1, 2, 3]
var arr2 = arr1
arr1[0]=100
console.log(arr1,arr2) // [ 100, 2, 3 ] [ 100, 2, 3 ]
//2蝇恶、slice
var arr3 = [1, 2, 3]
var arr4 = arr3.slice(-1) // 取數(shù)組最后一個元素
arr3[2] = 100
console.log(arr3,arr4) // [ 1, 2, 100 ] [ 3 ]
//看起來修改舊數(shù)組不改變新數(shù)組拳魁,像是深拷貝了
//但是!4榛 潘懊!
var arr5 = [1, 2, 3, {b: 4}]
var arr6 = arr5.slice(-1)
arr5[3].b = 100
console.log(arr5, arr6) //[ 1, 2, 3, { b: 100 } ] [ { b: 100 } ]
// 如果數(shù)組里元素是個引用類型,那么舊數(shù)組里這個元素被改變贿衍,會影響新數(shù)組
// 所以slice()方法是淺拷貝
//3授舟、concat 同上理
//4、遍歷
var arr7 = [1,2,3,{b:4}]
var arr8 = []
for (var i = 0; i < arr7.length; i ++) {
arr8.push(arr7[i])
}
arr7[3].b = 100
console.log(arr7, arr8) // [ 1, 2, 3, { b: 100 } ] [ 1, 2, 3, { b: 100 } ]
對象的淺拷貝
// 1贸辈、 對象淺拷貝 - Object.assign
function shallowCopy4(origin) {
return Object.assign({},origin)
}
//2释树、 對象淺拷貝 - 擴(kuò)展運(yùn)算符
// 擴(kuò)展運(yùn)算符(...)用于取出參數(shù)對象的所有可遍歷屬性,拷貝到當(dāng)前對象之中
function shallowCopy5(origin) {
return {
...origin
}
}
?深拷貝
1擎淤、最方便的JSON正反序列化
function deepClone1(origin) {
return JSON.parse(JSON.stringify(arr));
}
原理:利用 JSON.stringify 將js對象序列化(JSON字符串)奢啥,再使用JSON.parse來反序列化(還原)js對象
缺點(diǎn):缺點(diǎn)就是無法拷貝 undefined、function揉燃、symbol 這類特殊的屬性值扫尺,拷貝完變成null
2筋栋、遞歸實(shí)現(xiàn)
function DeepClone(originObj) {
// 先判斷obj是數(shù)組還是對象
let newObj;
if(originObj instanceof Array ) {
newObj = []
} else if (originObj instanceof Object) {
newObj = {}
}
for (let key in originObj) {
// 判斷子元素類型
if (typeof(originObj[key]) === "object") {
// 如果子元素為object 遞歸一下
newObj[key] = DeepClone(originObj[key])
} else {
newObj[key] = originObj[key]
}
}
return newObj
}