1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        Vue 源碼解析之工具方法

        共 7873字,需瀏覽 16分鐘

         ·

        2021-03-14 07:56


        如何定義一個(gè)空對(duì)象

        const emptyObject = Object.freeze({})

        isUndef

        檢測(cè) undefined, null

        function isUndef (v{
          return v === undefined || v === null
        }

        isDef

        檢測(cè)非 undefined, null

        function isDef (v{
          return v !== undefined && v !== null
        }

        isTrue

        檢測(cè) true

        function isTrue (v{
          return v === true
        }

        isFalse

        檢測(cè) false

        function isFalse (v{
          return v === false
        }

        isPrimitive

        檢測(cè)值是否是 Primitive

        function isPrimitive (value{
          return (
            typeof value === 'string' ||
            typeof value === 'number' ||
            // $flow-disable-line
            typeof value === 'symbol' ||
            typeof value === 'boolean'
          )
        }

        isObject

        檢測(cè)對(duì)象

        export function isObject (obj{
          return obj !== null && typeof obj === 'object'
        }

        toRawType

        const _toString = Object.prototype.toString

        export function toRawType (value{
          return _toString.call(value).slice(8-1)
        }

        isPlainObject

        export function isPlainObject (obj{
          return _toString.call(obj) === '[object Object]'
        }

        isRegExp

        檢測(cè)是否是正則表達(dá)式 export function isRegExp (v) { return _toString.call(v) === '[object RegExp]' }

        isValidArrayIndex

        檢測(cè)數(shù)組下標(biāo)是否越界

        export function isValidArrayIndex (val{
          const n = parseFloat(String(val))
          return n >= 0 && Math.floor(n) === n && isFinite(val)
        }

        isPromise

        export function isPromise (val{
          return (
            isDef(val) &&
            typeof val.then === 'function' &&
            typeof val.catch === 'function'
          )
        }

        toString

        export function toString (val{
          return val == null
            ? ''
            : Array.isArray(val) || (isPlainObject(val) && val.toString === _toString)
              ? JSON.stringify(val, null2)
              : String(val)
        }

        toNumber

        export function toNumber (val{
          const n = parseFloat(val)
          return isNaN(n) ? val : n
        }

        makeMap

        export function makeMap (
          str,
          expectsLowerCase
        {
          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]
        }

        isBuiltInTag

        export const isBuiltInTag = makeMap('slot,component'true)

        isReservedAttribute

        export const isReservedAttribute = makeMap('key,ref,slot,slot-scope,is')

        remove

        export function remove (arr, item{
          if (arr.length) {
            const index = arr.indexOf(item)
            if (index > -1) {
              return arr.splice(index, 1)
            }
          }
        }

        hasOwn

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

        cached

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

        camelize

        駝峰化

        const camelizeRE = /-(\w)/g
        export const camelize = cached((str) => {
          return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '')
        })

        capitalize

        export const capitalize = cached((str) => {
          return str.charAt(0).toUpperCase() + str.slice(1)
        })

        hyphenate


        const hyphenateRE = /\B([A-Z])/g
        export const hyphenate = cached((str) => {
          return str.replace(hyphenateRE, '-$1').toLowerCase()
        })

        bind

        function polyfillBind (fn, ctx{
          function boundFn (a{
            const l = arguments.length
            return l
              ? l > 1
                ? fn.apply(ctx, arguments)
                : fn.call(ctx, a)
              : fn.call(ctx)
          }

          boundFn._length = fn.length
          return boundFn
        }

        function nativeBind (fn, ctx{
          return fn.bind(ctx)
        }

        export const bind = Function.prototype.bind
          ? nativeBind
          : polyfillBind

        toArray

        將類似 array 轉(zhuǎn)成真正的數(shù)組

        export function toArray (list, start{
          start = start || 0
          let i = list.length - start
          const ret: Array<any> = new Array(i)
          while (i--) {
            ret[i] = list[i + start]
          }
          return ret
        }

        extend

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

        toObject

        /**

        • Merge an Array of Objects into a single Object. */
        export function toObject (arr{
          const res = {}
          for (let i = 0; i < arr.length; i++) {
            if (arr[i]) {
              extend(res, arr[i])
            }
          }
          return res
        }

        noop

        占位函數(shù)

        export function noop (a, b, c{}

        no

        /**
         * Always return false.
         */

        export const no = (a, b, c) => false

        identity

        /**
         * Return the same value.
         */

        export const identity = (_) => _

        once

        /**
         * Ensure a function is called only once.
         */

        export function once (fn: Function): Function {
          let called = false
          return function ({
            if (!called) {
              called = true
              fn.apply(thisarguments)
            }
          }
        }
        瀏覽 42
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            国产一区日韩 | 麻豆美女性感逼逼视频 | 正在播放露脸一区 | 小骚屄 | 日韩AV一区二区三区四区 | 激情开心五月天 | 男啪女色黄无遮挡免费观看 | 国产精品美女久久久久AV夜色 | 坐公交忘穿内裤被挺进老外文案 | 久久久久中文字幕 |