最近在深刻研究vue源碼,把學習過程當中,看到的一些好玩的的函數方法收集起來作分享,但願對你們對深刻學習js有所幫助。若是你們都能一眼看懂這些函數,說明技術仍是不錯的哦。html
Object.prototype.toString.call()返回的數據格式爲 [object Object]
類型,而後用slice截取第8位到倒一位,獲得結果爲 Object
vue
var _toString = Object.prototype.toString; function toRawType (value) { return _toString.call(value).slice(8, -1) }
運行結果測試es6
toRawType({}) // Object toRawType([]) // Array toRawType(true) // Boolean toRawType(undefined) // Undefined toRawType(null) // Null toRawType(function(){}) // Function
vue中判斷咱們寫的組件名是否是html
內置標籤的時候,若是用數組類遍歷那麼將要循環不少次獲取結果,若是把數組轉爲對象,把標籤名設置爲對象的key
,那麼不用依次遍歷查找,只須要查找一次就能獲取結果,提升了查找效率。數組
function makeMap (str, expectsLowerCase) { // 構建閉包集合map var map = Object.create(null); var list = str.split(','); for (var i = 0; i < list.length; i++) { map[list[i]] = true; } return expectsLowerCase ? function (val) { return map[val.toLowerCase()]; } : function (val) { return map[val]; } } // 利用閉包,每次判斷是不是內置標籤只需調用isHTMLTag var isHTMLTag = makeMap('html,body,base,head,link,meta,style,title') console.log('res', isHTMLTag('body')) // true
vue中_createElement
格式化傳入的children
的時候用到了simpleNormalizeChildren
函數,原來是爲了拍平數組,使二維數組扁平化,相似lodash
中的flatten
方法。緩存
// 先看lodash中的flatten _.flatten([1, [2, [3, [4]], 5]]) // 獲得結果爲 [1, 2, [3, [4]], 5] // vue中 function simpleNormalizeChildren (children) { for (var i = 0; i < children.length; i++) { if (Array.isArray(children[i])) { return Array.prototype.concat.apply([], children) } } return children } // es6中 等價於 function simpleNormalizeChildren (children) { return [].concat(...children) }
vue中利用Object.defineProperty
收集依賴,從而觸發更新視圖,可是數組卻沒法監測到數據的變化,可是爲何數組在使用push
pop
等方法的時候能夠觸發頁面更新呢,那是由於vue內部攔截了這些方法。閉包
// 重寫push等方法,而後再把原型指回原方法 var ARRAY_METHOD = [ 'push', 'pop', 'shift', 'unshift', 'reverse', 'sort', 'splice' ]; var array_methods = Object.create(Array.prototype); ARRAY_METHOD.forEach(method => { array_methods[method] = function () { // 攔截方法 console.log('調用的是攔截的 ' + method + ' 方法,進行依賴收集'); return Array.prototype[method].apply(this, arguments); } });
運行結果測試app
var arr = [1,2,3] arr.__proto__ = array_methods // 改變arr的原型 arr.unshift(6) // 打印結果: 調用的是攔截的 unshift 方法,進行依賴收集
vue中調用Vue.extend
實例化組件,Vue.extend
就是VueComponent
構造函數,而VueComponent
利用Object.create
繼承Vue,因此在日常開發中Vue
和 Vue.extend
區別不是很大。這邊主要學習用es5原生方法實現繼承的,固然了,es6中 class類直接用extends
繼承。函數
// 繼承方法 function inheritPrototype(Son, Father) { var prototype = Object.create(Father.prototype) prototype.constructor = Son // 把Father.prototype賦值給 Son.prototype Son.prototype = prototype } function Father(name) { this.name = name this.arr = [1,2,3] } Father.prototype.getName = function() { console.log(this.name) } function Son(name, age) { Father.call(this, name) this.age = age } inheritPrototype(Son, Father) Son.prototype.getAge = function() { console.log(this.age) }
運行結果測試學習
var son1 = new Son("AAA", 23) son1.getName() //AAA son1.getAge() //23 son1.arr.push(4) console.log(son1.arr) //1,2,3,4 var son2 = new Son("BBB", 24) son2.getName() //BBB son2.getAge() //24 console.log(son2.arr) //1,2,3
once
方法相對比較簡單,直接利用閉包實現就行了測試
function once (fn) { var called = false; return function () { if (!called) { called = true; fn.apply(this, arguments); } } }
簡單的深拷貝咱們能夠用 JSON.stringify()
來實現,不過vue源碼中的looseEqual
淺拷貝寫的也頗有意思,先類型判斷再遞歸調用,整體也不難,學一下思路。
function looseEqual (a, b) { if (a === b) { return true } var isObjectA = isObject(a); var isObjectB = isObject(b); if (isObjectA && isObjectB) { try { var isArrayA = Array.isArray(a); var isArrayB = Array.isArray(b); if (isArrayA && isArrayB) { return a.length === b.length && a.every(function (e, i) { return looseEqual(e, b[i]) }) } else if (!isArrayA && !isArrayB) { var keysA = Object.keys(a); var keysB = Object.keys(b); return keysA.length === keysB.length && keysA.every(function (key) { return looseEqual(a[key], b[key]) }) } else { /* istanbul ignore next */ return false } } catch (e) { /* istanbul ignore next */ return false } } else if (!isObjectA && !isObjectB) { return String(a) === String(b) } else { return false } } function isObject (obj) { return obj !== null && typeof obj === 'object' }
就先分享這些函數,其餘函數,後面繼續補充,若有不對歡迎指正,謝謝!