基礎函數

shared/util.js數組

1.hasOwn緩存

var hasOwnProperty = Object.prototype.hasOwnProperty;
function hasOwn (obj, key) {
    return hasOwnProperty.call(obj, key)
}

extend 函數

export function extend (to: Object, _from: ?Object): Object {
  for (const key in _from) {
    to[key] = _from[key]
  }
  return to
}

mergeFieldoop

// 對option[key]進行策略處理,默認不處理
function mergeField (key) {
    const strat = strats[key] || defaultStrat
    options[key] = strat(parent[key], child[key], vm, key)
  }

makeMapthis

export function makeMap (
  str: string,
  expectsLowerCase?: boolean
): (key: string) => true | void {
  const map = Object.create(null)
  const list: Array<string> = str.split(',')
  for (let i = 0; i < list.length; i++) {
    map[list[i]] = true
  }
  // 箭頭函數返回的格式
  return expectsLowerCase
    ? val => map[val.toLowerCase()]
    : val => map[val]
} 

2.getTypeIndexspa

function getType (fn) {
  var match = fn && fn.toString().match(/^\s*function (\w+)/);
  return match ? match[1] : ''   //得到函數名
}
function isSameType (a, b) {
return getType(a) === getType(b)
}
function getTypeIndex (type, expectedTypes) { if (!Array.isArray(expectedTypes)) { return isSameType(expectedTypes, type) ? 0 : -1   } for (var i = 0, len = expectedTypes.length; i < len; i++) { if (isSameType(expectedTypes[i], type)) { return i } } return -1 }

3.proxy代理prototype

//key a
if (!(key in vm)) {
  proxy(vm, "_props", key);
}
function noop (a, b, c) {}
var sharedPropertyDefinition = {
    enumerable: true,
    configurable: true,
    get: noop,
    set: noop
};

function proxy (target, sourceKey, key) {
    sharedPropertyDefinition.get = function proxyGetter () {
      // this爲vm。
       return this[sourceKey][key]
     };
    sharedPropertyDefinition.set = function proxySetter (val) {
       this[sourceKey][key] = val;
    };
   Object.defineProperty(target, key, sharedPropertyDefinition);
}
//對vm._props進行代理。

4.toggleObserving代理

var shouldObserve = true;
function toggleObserving (value) {
    shouldObserve = value;
}

 5.hasProtocode

var hasProto = '__proto__' in {};console.log(hasProto); //true

 6.mergeOptionscomponent

export function mergeOptions (
  parent: Object,
  child: Object,
  vm?: Component
): Object {
  if (process.env.NODE_ENV !== 'production') {
    //覈實子對象中components的名字合法
    checkComponents(child)
  }

  if (typeof child === 'function') {
    child = child.options
  }

  normalizeProps(child, vm)
  normalizeInject(child, vm)
  normalizeDirectives(child)
  const extendsFrom = child.extends
  if (extendsFrom) {
    parent = mergeOptions(parent, extendsFrom, vm)
  }
  if (child.mixins) {
    for (let i = 0, l = child.mixins.length; i < l; i++) {
      parent = mergeOptions(parent, child.mixins[i], vm)
    }
  }
  const options = {}
  let key
  for (key in parent) {
    mergeField(key)
  }
  for (key in child) {
    if (!hasOwn(parent, key)) {
      mergeField(key)
    }
  }
  function mergeField (key) {
    const strat = strats[key] || defaultStrat
    options[key] = strat(parent[key], child[key], vm, key)
  }
  return options
} 

 7.toArray、cached(shared/util.js)

// 造成一個新數組,該數組是 any 從 number 之後的數組。
export function toArray (list: any, start?: number): Array<any> {
  start = start || 0
  let i = list.length - start
  const ret: Array<any> = new Array(i)
  while (i--) {
    ret[i] = list[i + start]
  }
  return ret
}

// 高階函數,對外賦值以後調用。每次調用緩存結果。

export function cached<F: Function> (fn: F): F {   const cache = Object.create(null)   return (function cachedFn (str: string) {     const hit = cache[str]     return hit || (cache[str] = fn(str))   }: any)}

相關文章
相關標籤/搜索