企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 3 [\core\util\] 基础工具目录 ### 3-1 目录文件 ### 3-2 (index.js)入口文件 >[export] ~~~ ;(导出)工具接口 export * from 'shared/util' export * from './lang' export * from './env' export * from './options' export * from './debug' export * from './props' export { defineReactive } from '../observer/index' ~~~ ### 3-3 (lang.js)语言扩展工具 >[info] module ~~~ ;字符串是否以$或_开头 export function isReserved (str) { const c = (str + '').charCodeAt(0) return c === 0x24 || c === 0x5F } ;对象属性定义 export function def (obj, key, val, enumerable) { Object.defineProperty(obj, key, { value: val, enumerable: !!enumerable, writable: true, configurable: true }) } ;路径解析 const bailRE = /[^\w\.]/ export function parsePath (path) { if (bailRE.test(path)) { return } else { path = path.split('.') return function (obj) { for (let i = 0; i < path.length; i++) { if (!obj) return obj = obj[path[i]] } return obj } } } ~~~ >[info] export ~~~ ;(导出)检查字符串是否以$或_开头 export function isReserved (str) {} ;(导出)定义对象的一个属性 export function def (obj, key, val, enumerable){} ;(导出)点路径解析 export function parsePath (path) {} ~~~ ### 7-4 (env.js)环境工具 >[info] module ~~~ ;检查简单对象是否有__proto__属性 export const hasProto = '__proto__' in {} ;检查是否浏览器运行环境 export const inBrowser = typeof window !== 'undefined' && Object.prototype.toString.call(window) !== '[object Object]' ;开发工具 export const devtools = inBrowser && window.__VUE_DEVTOOLS_GLOBAL_HOOK__ ;浏览器客户端类型 const UA = inBrowser && window.navigator.userAgent.toLowerCase() const isIos = UA && /(iphone|ipad|ipod|ios)/i.test(UA) const isWechat = UA && UA.indexOf('micromessenger') > 0 ;异步运行 export const nextTick = (function () { let callbacks = [] let pending = false let timerFunc ;异步调用接口 function nextTickHandler () { pending = false const copies = callbacks.slice(0) callbacks = [] for (let i = 0; i < copies.length; i++) { copies[i]() } } ;生成异步执行函数timerFunc if (typeof MutationObserver !== 'undefined' && !(isWechat && isIos)) { let counter = 1 const observer = new MutationObserver(nextTickHandler) const textNode = document.createTextNode(counter) observer.observe(textNode, { characterData: true }) timerFunc = function () { counter = (counter + 1) % 2 textNode.data = counter } } else { const context = inBrowser ? window : typeof global !== 'undefined' ? global : {} timerFunc = context.setImmediate || setTimeout } ;返回异步执行函数 return function (cb, ctx) { const func = ctx ? function () { cb.call(ctx) } : cb callbacks.push(func) if (pending) return pending = true timerFunc(nextTickHandler, 0) } })() ;环境表的添加,检查,清空操作 let _Set if (typeof Set !== 'undefined' && Set.toString().match(/native code/)) { _Set = Set } else { _Set = function () { this.set = Object.create(null) } _Set.prototype.has = function (key) { return this.set[key] !== undefined } _Set.prototype.add = function (key) { this.set[key] = 1 } _Set.prototype.clear = function () { this.set = Object.create(null) } } ;导出环境表接口 export { _Set } ~~~ >[info] export ~~~ ;(导出)是否可以使用__proto__ export const hasProto ;(导出)是否浏览器运行环境 export const inBrowser ;(导出)开发环境工具 export const devtools ;(导出)异步执行 export nextTick{} ;(导出)环境集合 export { _Set } ~~~ ### 3-5 (options.js) ### 3-6 (debug.js) ### 3-6 (props.js) ### 3-7 (defineReactive) ![](https://box.kancloud.cn/2016-05-06_572bf6c0146bf.png)