常見值型別: 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;//特殊參考型別,指標指向為空 // 特殊參考型別,但不用于存盤資料,所以沒有“拷貝,復制函式”這一說 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、識別函式 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' /*判斷函式*/ function fn(){} console.log(typeof fn); // 'function' /*判斷是否是參考型別 (不可再細分)*/ console.log(typeof null); // 'object' console.log(typeof []); // 'object' console.log(typeof {}); // 'object'View Code
深拷貝:
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修改的內容會影響obj的內容,因為他們修改的都是同一個堆內容 * */ 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 } // 初始化回傳結果 let result; if (obj instanceof Array) { result = [] } else { result = {} } // 遍歷obj for (let key in obj) { // 保證key不是原型的屬性 if (obj.hasOwnProperty(key)) { // 遞回呼叫 result[key] = deepClone(obj[key]) } } return result }View Code
型別轉換常考考點: 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){} // 相當于: 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 邏輯判斷 與或非 && || !
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/14881.html
標籤:JavaScript
