常見值類型:
let a; //undefined
let s = 'abc';
let n = 100;
let b = true;
let sb = Symbol('s');
let nn = NaN
常見引用類型:
const obj = {x: 100};
const arr = [1, 2, 3];
const n = null;//特殊引用類型,指針指向為空
// 特殊引用類型,但不用于存儲數(shù)據(jù),所以沒有“拷貝,復(fù)制函數(shù)”這一說
function fn(){}
console.log(typeof obj) //obj
console.log(typeof arr) //obj
console.log(typeof n) //obj
console.log(typeof fn) //function
typeof運算符:
1、識別所有的值類型
2、識別函數(shù)
3、判斷是否是引用類型 (不可再細分)
![]() let a; //undefined let s = 'abc'; let n = 100; let b = true; let sb = Symbol('s'); console.log(typeof a);// 'undefined' console.log(typeof s);// 'string' console.log(typeof n);// 'number' console.log(typeof b);// 'boolean' console.log(typeof sb);// 'symbol' /*判斷函數(shù)*/ function fn(){} console.log(typeof fn); // 'function' /*判斷是否是引用類型 (不可再細分)*/ console.log(typeof null); // 'object' console.log(typeof []); // 'object' console.log(typeof {}); // 'object'
深拷貝:
![]() const obj = { a: 100, b: { b1: [1, 2, 3], b2: 'string' }, c: ['a', 'b', 'c'] } /* * 沒做深拷貝的效果 const obj2 = obj obj2.a = 200 obj2.b.b2 = 'abc123' obj2.c[0] = 'aa' console.log(obj) console.log(obj2) obj2修改的內(nèi)容會影響obj的內(nèi)容,因為他們修改的都是同一個堆內(nèi)容 * */ const obj2 = deepClone(obj); obj2.a = 200 obj2.b.b2 = 'abc123' obj2.c[0] = 'aa' console.log(obj) console.log(obj2) /** * 深拷貝 * @param {Object} obj 要深拷貝的對象 * */ function deepClone(obj = {}) { // obj如果不是引用類型,或者是null,直接返回 if (typeof obj !== 'object' || obj == null) { return obj } // 初始化返回結(jié)果 let result; if (obj instanceof Array) { result = [] } else { result = {} } // 遍歷obj for (let key in obj) { // 保證key不是原型的屬性 if (obj.hasOwnProperty(key)) { // 遞歸調(diào)用 result[key] = deepClone(obj[key]) } } return result }
類型轉(zhuǎn)換常考考點:
1、字符串拼接
let a = 100 + 10;//110
let b = 100 + '10';// '10010'
let c = true + '10';// 'true10'
2、==運算符
100 == '100' // true
0 == '' // true
0 == false // true
false == '' // true
null == undefined // true
// ==運算符的使用場景
// 除了==null之外,其他一律都用 === ,例如:
const obj = {x:100}
if(obj.a == null){}
// 相當(dāng)于: if(obj.a === null || obj.a === undefined){}
3、if語句和邏輯運算
truly變量:!!a === true 的變量
falsely變量:!!a === false 的變量
以下是falsely變量,除此之外都是truly變量
/*
* !!0 === false
* !!NaN === false
* !!'' === false
* !!null === false
* !!undefined === false
* !!false === false
* */
在if語句中的判斷就是判斷是truly變量還是falsely變量。truly變量就是為真,falsely變量就是為false
邏輯判斷 與或非 && || !
|
|