Table of Contents
EMPTY_OBJ & EMPTY_ARR
NOOP
NO
isOn
isModelListener
extend
remove
hasOwn
isArray
isMap & isSet & isDate & isRegExp
isFunction & isString & isSymbol & isObject
isPromise
objectToString
toTypeString
toRawType
isPlainObject
isIntegerKey
isReservedProp
isBuiltInDirective
cacheStringFunction
camelize
capitalize
toHandlerKey
hasChanged
invokeArrayFns
def
looseToNumber
toNumber
getGlobalThis
genPropsAccessExp
总结
Home Web Front-end Vue.js Let's talk about the 38 tool functions under the Vue3 shared module (source code reading)

Let's talk about the 38 tool functions under the Vue3 shared module (source code reading)

Dec 12, 2022 pm 07:55 PM
vue vue3 shared module

Compared with the tool functions of

Vue2Let's talk about the 38 tool functions under the Vue3 shared module (source code reading), the tool functions of

##Vue3 have changed a lot. My personal feeling is mainly reflected in the syntax. Fully embrace es6; [Related recommendations: vuejs video tutorial, web front-end development]

Compared with the tool class, there are no functional changes How much, most of them are basically the same, but there are slight differences in syntax and implementation;

Source code address:

  • vue-next
  • vue-next/packages/shared/src/index.ts
I have introduced many reading methods before, but this time I skipped it and started directly. Read the source code;

All tool functions

  • makeMap: Generate an object similar to Set, use Used to determine whether a certain value exists
  • EMPTY_OBJ: Empty object
  • EMPTY_ARR: Empty array
  • NOOP: Empty function
  • NO: Function that returns false
  • isOn: Determine whether it is on Events starting with
  • isModelListener: Determine the string starting with onUpdate
  • extend: Merge objects
  • remove: Remove a value from the array
  • hasOwn: Determine whether the object has a certain attribute
  • isArray: Determines whether it is an array
  • isMap: Determines whether it is Map
  • ##isSet
  • : Determines whether it is an array Set
  • isDate
  • : Determine whether it is Date
  • isRegExp
  • : Determine whether it is RegExp
  • isFunction
  • : Determine whether it is a function
  • isString
  • : Determine whether it is a string
  • isSymbol
  • : Determine whether it is Symbol
  • isObject
  • : Determine whether it is an object
  • isPromise
  • : Determine whether Is Promise
  • objectToString
  • : Object.prototype.toString##toTypeString
  • :
  • Abbreviation of Object.prototype.toStringtoRawType
  • : Get the type of object
  • isPlainObject
  • : Determine whether it is a normal object
  • isIntegerKey
  • : Determine whether it is an integer
  • keyisReservedProp
  • : Determine whether it is a reserved property
  • isBuiltInDirective
  • : Determine whether it is a built-in instruction
  • camelize
  • : Convert the string to camel case
  • hyphenate
  • : Convert the string to concatenated case Characters
  • capitalize
  • : Capitalize the first letter of the string
  • toHandlerKey
  • : Convert the string to the
  • key of the event handler hasChanged
  • : Determine whether two values ​​are equal
  • invokeArrayFns
  • : Call the function in the array
  • def
  • : Define the properties of the object
  • looseToNumber
  • : Convert a string to a number
  • toNumber
  • : Convert a string to a number
  • getGlobalThis
  • : Get the global object
  • genPropsAccessExp
  • : Generate the access expression of
  • propsthis Most of them are the same as the tool functions of
  • Vue2
, and the judgment of data type is done in the same way. Because of the previous reading experience of

Vue2, this Quick reading; If you want more details, you can read the article I wrote before: [Source code reading] Analysis of 36 utility functions in the Vue2 source code shared module

;

And this time it is the direct source code, the ts

version, which is no longer processed into

js, so read the ts source code directly; Official start

makeMap

export function makeMap(
  str: string,
  expectsLowerCase?: boolean
): (key: string) => boolean {
  const map: Record<string, boolean> = Object.create(null)
  const list: Array<string> = str.split(&#39;,&#39;)
  for (let i = 0; i < list.length; i++) {
    map[list[i]] = true
  }
  return expectsLowerCase ? val => !!map[val.toLowerCase()] : val => !!map[val]
}
Copy after login
makeMap

The source code is in the same directory as

makeMap.ts file, directly use the export keyword to export after importing it. The implementation method is the same as the implementation method of Vue2;

EMPTY_OBJ & EMPTY_ARR

export const EMPTY_OBJ: { readonly [key: string]: any } = __DEV__
  ? Object.freeze({})
  : {}
export const EMPTY_ARR = __DEV__ ? Object.freeze([]) : []
Copy after login

EMPTY_OBJEMPTY_ARR的实现方式和Vue2emptyObject相同,都是使用Object.freeze冻结对象,防止对象被修改;

NOOP

export const NOOP = () => {}
Copy after login

Vue2noop实现方式相同,都是一个空函数,移除了入参;

NO

/**
 * Always return false.
 */
export const NO = () => false
Copy after login

Vue2no实现方式相同,都是一个返回false的函数,移除了入参;

isOn

const onRE = /^on[^a-z]/
export const isOn = (key: string) => onRE.test(key)
Copy after login

判断是否是on开头的事件,并且on后面的第一个字符不是小写字母;

isModelListener

export const isModelListener = (key: string) => key.startsWith(&#39;onUpdate:&#39;)
Copy after login

判断是否是onUpdate:开头的字符串;

参考:startWith

extend

export const extend = Object.assign
Copy after login

直接拥抱es6Object.assignVue2的实现方式是使用for in循环;

remove

export const remove = <T>(arr: T[], el: T) => {
  const i = arr.indexOf(el)
  if (i > -1) {
    arr.splice(i, 1)
  }
}
Copy after login

对比于Vue2删除了一些代码,之前的快速删除最后一个元素的判断不见了;

猜测可能是因为有bug,因为大家都知道Vue2的数组响应式必须使用Arrayapi,那样操作可能会导致数组响应式失效;

hasOwn

const hasOwnProperty = Object.prototype.hasOwnProperty
export const hasOwn = (
  val: object,
  key: string | symbol
): key is keyof typeof val => hasOwnProperty.call(val, key)
Copy after login

使用的是Object.prototype.hasOwnProperty,和Vue2相同;

isArray

export const isArray = Array.isArray
Copy after login

使用的是Array.isArray,和Vue2相同;

isMap & isSet & isDate & isRegExp

export const isMap = (val: unknown): val is Map<any, any> =>
  toTypeString(val) === &#39;[object Map]&#39;
export const isSet = (val: unknown): val is Set<any> =>
  toTypeString(val) === &#39;[object Set]&#39;

export const isDate = (val: unknown): val is Date =>
  toTypeString(val) === &#39;[object Date]&#39;
export const isRegExp = (val: unknown): val is RegExp =>
  toTypeString(val) === &#39;[object RegExp]&#39;
Copy after login

都是使用Object.toString来判断类型,对比于Vue2新增了isMapisSetisDate,实现方式没变;

isFunction & isString & isSymbol & isObject

export const isFunction = (val: unknown): val is Function =>
  typeof val === &#39;function&#39;
export const isString = (val: unknown): val is string => typeof val === &#39;string&#39;
export const isSymbol = (val: unknown): val is symbol => typeof val === &#39;symbol&#39;
export const isObject = (val: unknown): val is Record<any, any> =>
  val !== null && typeof val === &#39;object&#39;
Copy after login

Vue2的实现方式相同,都是使用typeof来判断类型,新增了isSymbol

isPromise

export const isPromise = <T = any>(val: unknown): val is Promise<T> => {
  return isObject(val) && isFunction(val.then) && isFunction(val.catch)
}
Copy after login

Vue2对比修改了实现方式,但是判断逻辑没变;

objectToString

export const objectToString = Object.prototype.toString
Copy after login

直接是Object.prototype.toString

toTypeString

export const toTypeString = (value: unknown): string =>
  objectToString.call(value)
Copy after login

对入参执行Object.prototype.toString

toRawType

export const toRawType = (value: unknown): string => {
  // extract "RawType" from strings like "[object RawType]"
  return toTypeString(value).slice(8, -1)
}
Copy after login

Vue2的实现方式相同;

isPlainObject

export const isPlainObject = (val: unknown): val is object =>
  toTypeString(val) === &#39;[object Object]&#39;
Copy after login

Vue2的实现方式相同;

isIntegerKey

export const isIntegerKey = (key: unknown) =>
  isString(key) &&
  key !== &#39;NaN&#39; &&
  key[0] !== &#39;-&#39; &&
  &#39;&#39; + parseInt(key, 10) === key
Copy after login

判断一个字符串是不是由一个整数组成的;

isReservedProp

export const isReservedProp = /*#__PURE__*/ makeMap(
  // the leading comma is intentional so empty string "" is also included
  &#39;,key,ref,ref_for,ref_key,&#39; +
    &#39;onVnodeBeforeMount,onVnodeMounted,&#39; +
    &#39;onVnodeBeforeUpdate,onVnodeUpdated,&#39; +
    &#39;onVnodeBeforeUnmount,onVnodeUnmounted&#39;
)
Copy after login

使用makeMap生成一个对象,用于判断入参是否是内部保留的属性;

isBuiltInDirective

export const isBuiltInDirective = /*#__PURE__*/ makeMap(
  &#39;bind,cloak,else-if,else,for,html,if,model,on,once,pre,show,slot,text,memo&#39;
)
Copy after login

使用makeMap生成一个对象,用于判断入参是否是内置的指令;

cacheStringFunction

const cacheStringFunction = <T extends (str: string) => string>(fn: T): T => {
  const cache: Record<string, string> = Object.create(null)
  return ((str: string) => {
    const hit = cache[str]
    return hit || (cache[str] = fn(str))
  }) as T
}
Copy after login

Vue2cached相同,用于缓存字符串;

camelize

const camelizeRE = /-(\w)/g
/**
 * @private
 */
export const camelize = cacheStringFunction((str: string): string => {
  return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : &#39;&#39;))
})
Copy after login

-连接的字符串转换为驼峰式,同Vue2camelize相同;

capitalize

const hyphenateRE = /\B([A-Z])/g
/**
 * @private
 */
export const hyphenate = cacheStringFunction((str: string) =>
  str.replace(hyphenateRE, &#39;-$1&#39;).toLowerCase()
)
Copy after login

将驼峰式字符串转换为-连接的字符串,同Vue2hyphenate相同;

capitalize

/**
 * @private
 */
export const capitalize = cacheStringFunction(
  (str: string) => str.charAt(0).toUpperCase() + str.slice(1)
)
Copy after login

将字符串首字母大写,同Vue2capitalize相同;

toHandlerKey

/**
 * @private
 */
export const toHandlerKey = cacheStringFunction((str: string) =>
  str ? `on${capitalize(str)}` : ``
)
Copy after login

将字符串首字母大写并在前面加上on

hasChanged

// compare whether a value has changed, accounting for NaN.
export const hasChanged = (value: any, oldValue: any): boolean =>
  !Object.is(value, oldValue)
Copy after login

Vue2相比,移除了polyfill,直接使用Object.is

invokeArrayFns

export const invokeArrayFns = (fns: Function[], arg?: any) => {
  for (let i = 0; i < fns.length; i++) {
    fns[i](arg)
  }
}
Copy after login

批量调用传递过来的函数列表,如果有参数,会将参数传递给每个函数;

def

export const def = (obj: object, key: string | symbol, value: any) => {
  Object.defineProperty(obj, key, {
    configurable: true,
    enumerable: false,
    value
  })
}
Copy after login

使用Object.defineProperty定义一个属性,并使这个属性不可枚举;

looseToNumber

/**
 * "123-foo" will be parsed to 123
 * This is used for the .number modifier in v-model
 */
export const looseToNumber = (val: any): any => {
  const n = parseFloat(val)
  return isNaN(n) ? val : n
}
Copy after login

将字符串转换为数字,如果转换失败,返回原字符串;

通过注释知道主要用于v-model.number修饰符;

toNumber

/**
 * Only conerces number-like strings
 * "123-foo" will be returned as-is
 */
export const toNumber = (val: any): any => {
  const n = isString(val) ? Number(val) : NaN
  return isNaN(n) ? val : n
}
Copy after login

将字符串转换为数字,如果转换失败,返回原数据;

getGlobalThis

let _globalThis: any
export const getGlobalThis = (): any => {
  return (
    _globalThis ||
    (_globalThis =
      typeof globalThis !== &#39;undefined&#39;
        ? globalThis
        : typeof self !== &#39;undefined&#39;
        ? self
        : typeof window !== &#39;undefined&#39;
        ? window
        : typeof global !== &#39;undefined&#39;
        ? global
        : {})
  )
}
Copy after login

获取全局对象,根据环境不同返回的对象也不同;

genPropsAccessExp

const identRE = /^[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*$/

export function genPropsAccessExp(name: string) {
  return identRE.test(name)
    ? `__props.${name}`
    : `__props[${JSON.stringify(name)}]`
}
Copy after login

生成props的访问表达式,如果name是合法的标识符,直接返回__props.name,否则返回通过JSON.stringify转换后的__props[name]

总结

通过这次的源码阅读,我们巩固了一些基础知识,通过对比Vue2的工具函数,我们也了解了Vue3的一些变化;

这些变化个人感觉主要集中在拥抱es6,可以看到放弃ie是多么自由而奔放;

话外题,不知道大家有没有发现MDN上面的浏览器兼容性表格,已经没有了ie的相关信息。

(学习视频分享:vuejs入门教程编程基础视频

The above is the detailed content of Let's talk about the 38 tool functions under the Vue3 shared module (source code reading). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to add functions to buttons for vue How to add functions to buttons for vue Apr 08, 2025 am 08:51 AM

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to reference js file with vue.js How to reference js file with vue.js Apr 07, 2025 pm 11:27 PM

There are three ways to refer to JS files in Vue.js: directly specify the path using the &lt;script&gt; tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

How to use watch in vue How to use watch in vue Apr 07, 2025 pm 11:36 PM

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

How to return to previous page by vue How to return to previous page by vue Apr 07, 2025 pm 11:30 PM

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses &lt;router-link to=&quot;/&quot; component window.history.back(), and the method selection depends on the scene.

Vue realizes marquee/text scrolling effect Vue realizes marquee/text scrolling effect Apr 07, 2025 pm 10:51 PM

Implement marquee/text scrolling effects in Vue, using CSS animations or third-party libraries. This article introduces how to use CSS animation: create scroll text and wrap text with &lt;div&gt;. Define CSS animations and set overflow: hidden, width, and animation. Define keyframes, set transform: translateX() at the beginning and end of the animation. Adjust animation properties such as duration, scroll speed, and direction.

How to use vue pagination How to use vue pagination Apr 08, 2025 am 06:45 AM

Pagination is a technology that splits large data sets into small pages to improve performance and user experience. In Vue, you can use the following built-in method to paging: Calculate the total number of pages: totalPages() traversal page number: v-for directive to set the current page: currentPage Get the current page data: currentPageData()

How to use function intercept vue How to use function intercept vue Apr 08, 2025 am 06:51 AM

Function interception in Vue is a technique used to limit the number of times a function is called within a specified time period and prevent performance problems. The implementation method is: import the lodash library: import { debounce } from 'lodash'; Use the debounce function to create an intercept function: const debouncedFunction = debounce(() =&gt; { / Logical / }, 500); Call the intercept function, and the control function is called at most once in 500 milliseconds.

See all articles