shared/util.js
1.hasown
var hasownproperty =object.prototype.hasownproperty;function
hasown (obj, key)
extend
export function extend (to: object, _from: ?object): objectreturn
to}
mergefield
//對option[key]進行策略處理,預設不處理
function
mergefield (key)
makemap
export functionmakemap (
str: string,
expectslowercase?: boolean
): (key: string) => true | void
//箭頭函式返回的格式
return
expectslowercase
? val =>map[val.tolowercase()]
: val =>map[val]
}
很多地方都會用到:例如」isplaintextelement = makemap('script,style,textarea', true)
2.gettypeindex
functiongettype (fn)
// console.log(boolean.tostring().match(/^\s*function (\w+)/))
// ["function boolean", "boolean", index: 0, input: "function boolean() ", groups: undefined]
functionissametype (a, b)
functiongettypeindex (type, expectedtypes)
for (var i = 0, len = expectedtypes.length; i < len; i++)
} return -1}
3.proxy**
//key a
if (!(key in
vm))
function
noop (a, b, c) {}
var sharedpropertydefinition =;
function
proxy (target, sourcekey, key) ;
sharedpropertydefinition.set = function
proxysetter (val) ;
object.defineproperty(target, key, sharedpropertydefinition);}//
對vm._props進行**。
4.toggleobserving
var shouldobserve = true;function
toggleobserving (value)
5.hasproto
var hasproto = '__proto__' in {};console.log(hasproto); //true
6.mergeoptions
export functionmergeoptions (
parent: object,
child: object,
vm?: component
): object
if (typeof child === 'function')
normalizeprops(child, vm)
normalizeinject(child, vm)
normalizedirectives(child)
const extendsfrom =child.extends
if(extendsfrom)
if(child.mixins)
} const options ={}
let key
for (key in
parent)
for (key in
child)
} function
mergefield (key)
return
options
}
7.toarray、cached(shared/util.js)
//// 高階函式,對外賦值之後呼叫。每次呼叫快取結果。形成乙個新陣列,該陣列是 any 從 number 以後的陣列。
export function toarray (list: any, start?: number): array
return
ret}
export function cached(fn: f): f : any)
}cached的用法如下:
functioncached(fn) )
}let f = function
(x)
let k1 =cached(f)
let k2 = k1('7')
console.log('k2=',k2) //
k2= 49
let k3 = k1('7')
console.log('k3=',k3) //
k3=49
以 _和$開頭的變數或者方法的key,
/** * check if a string starts with $ or _ */
export
function isreserved (str: string): boolean
Vue3讀原始碼 1
要讀懂原始碼還是要有一定的基礎的,在此就預設是會的。我使用的工具是vscode,廢話不多說,首先看最外層的函式 var vue function exports 可以看到,vue是 裡的自執行函式的返回值exports,是個物件。exports是函式接收乙個空物件引數然後經過函式處理後返回的。那麼函...
讀Muduo原始碼筆記 1
物件銷毀時出現的競態條件 執行緒安全的類 簡單的執行緒安全類 class counter int value const int getandincrease private int value mutable mutexlock mutex int counter value const int ...
C 讀Autofac原始碼筆記(1)
最近在看autofac的原始碼。autofac據說是.net中最快的ioc框架,具體沒有實驗,於是看看autofac具體是怎樣例項化實體。如上圖所示,autofac使用的是表示式樹來建立實體。expression tree建立實體的效能比activator.createinstance要高。網上有人...