RT
我在 utils/index
暴露了当前所有的文件函数 如下
但是我在其他文件引用的时候,出现了这样的情况
也就是说import {addPropsHook} from '../utils/index' // 获取不到 显示undefined
import {addPropsHook} from '../utils/helper' // 可以正确引入funciton
可我已经在 index.js
导出 helper.js
了
目录层级
helper.js
import { isObject, isArray, now, isArguments, getURL } from './index'
import { checkOption } from '../variables'
import { sd } from '../sensorsdata.full'
export const age = 12
export const getRandomBasic = (number) => {
var today = new Date()
var seed = today.getTime()
function rnd() {
seed = (seed * 9301 + 49297) % 233280
return seed / 233280.0
}
return Math.ceil(rnd() * number)
}
export const getRandom = () => {
if (typeof Uint32Array === 'function') {
var cry = ''
if (typeof crypto !== 'undefined') {
cry = crypto
} else if (typeof window.msCrypto !== 'undefined') {
cry = window.msCrypto
}
if (isObject(cry) && cry.getRandomValues) {
var typedArray = new Uint32Array(1)
var randomNumber = cry.getRandomValues(typedArray)[0]
var integerLimit = Math.pow(2, 32)
return randomNumber / integerLimit
}
}
return getRandomBasic(10000000000000000000) / 10000000000000000000
}
export const UUID = () => {
var T = function() {
var d = 1 * new Date()
var i = 0
while (d === 1 * new Date()) {
i++
}
return d.toString(16) + i.toString(16)
}
var R = function() {
return getRandom().toString(16).replace('.', '')
}
var UA = function() {
var ua = navigator.userAgent
var i
var ch
var buffer = []
var ret = 0
function xor(result, byte_array) {
var j
var tmp = 0
for (j = 0; j < byte_array.length; j++) {
tmp |= buffer[j] << (j * 8)
}
return result ^ tmp
}
for (i = 0; i < ua.length; i++) {
ch = ua.charCodeAt(i)
buffer.unshift(ch & 0xff)
if (buffer.length >= 4) {
ret = xor(ret, buffer)
buffer = []
}
}
if (buffer.length > 0) {
ret = xor(ret, buffer)
}
return ret.toString(16)
}
return function() {
var se = String(screen.height * screen.width)
if (se && /\d{5,}/.test(se)) {
se = se.toString(16)
} else {
se = String(getRandom() * 31242)
.replace('.', '')
.slice(0, 8)
}
var val = T() + '-' + R() + '-' + UA() + '-' + se + '-' + T()
if (val) {
return val
} else {
return (String(getRandom()) + String(getRandom()) + String(getRandom())).slice(2, 15)
}
}
}
const nativeForEach = Array.prototype.forEach
const hasOwnProperty$2 = Object.prototype.hasOwnProperty
const hasOwnProperty$1 = Object.prototype.hasOwnProperty
export const each = (obj, iterator, context) => {
if (obj == null) {
return false
}
if (nativeForEach && obj.forEach === nativeForEach) {
obj.forEach(iterator, context)
} else if (isArray(obj)) {
for (var i = 0, l = obj.length; i < l; i++) {
i in obj && iterator.call(context, obj[i], i, obj)
}
} else {
for (var key in obj) {
if (hasOwnProperty$2.call(obj, key)) {
iterator.call(context, obj[key], key, obj)
}
}
}
}
export function extend(obj) {
each(Array.prototype.slice.call(arguments, 1), function(source) {
for (var prop in source) {
if (hasOwnProperty$1.call(source, prop) && source[prop] !== void 0) {
obj[prop] = source[prop]
}
}
})
return obj
}
export function trim(str) {
return str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '')
}
export function safeJSONParse(str) {
var val = null
try {
val = JSON.parse(str)
} catch (e) {
console.log('🚀 ~ safeJSONParse ~ e', e)
}
return val
}
export function map(obj, iterator) {
var results = []
if (obj == null) {
return results
}
if (Array.prototype.map && obj.map === Array.prototype.map) {
return obj.map(iterator)
}
each(obj, function(value, index, list) {
results.push(iterator(value, index, list))
})
return results
}
export function base64Decode(str) {
var arr = []
try {
arr = map(atob(str).split(''), function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
})
} catch (e) {
arr = []
}
try {
return decodeURIComponent(arr.join(''))
} catch (e) {
return arr.join('')
}
}
export function base64Encode(str) {
var result = ''
try {
result = btoa(
encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
return String.fromCharCode('0x' + p1)
})
)
} catch (e) {
result = str
}
return result
}
export function extend2Lev(obj) {
each(Array.prototype.slice.call(arguments, 1), function(source) {
for (var prop in source) {
if (source[prop] !== void 0) {
if (isObject(source[prop]) && isObject(obj[prop])) {
extend(obj[prop], source[prop])
} else {
obj[prop] = source[prop]
}
}
}
})
return obj
}
export function filter(arr, fn, context) {
var hasOwn = Object.prototype.hasOwnProperty
if (arr.filter) {
return arr.filter(fn)
}
var ret = []
for (var i = 0; i < arr.length; i++) {
if (!hasOwn.call(arr, i)) {
continue
}
var val = arr[i]
if (fn.call(context, val, i, arr)) {
ret.push(val)
}
}
return ret
}
export function formatJsonString(obj) {
try {
return JSON.stringify(obj, null, ' ')
} catch (e) {
return JSON.stringify(obj)
}
}
export function indexOf(arr, target) {
var indexof = arr.indexOf
if (indexof) {
return indexof.call(arr, target)
} else {
for (var i = 0; i < arr.length; i++) {
if (target === arr[i]) {
return i
}
}
return -1
}
}
export function inherit(subclass, Superclass) {
subclass.prototype = new Superclass()
subclass.prototype.constructor = subclass
subclass.superclass = Superclass.prototype
return subclass
}
export function hasAttribute(ele, attrName) {
if (ele.hasAttribute) {
return ele.hasAttribute(attrName)
} else if (ele.attributes) {
return !!(ele.attributes[attrName] && ele.attributes[attrName].specified)
}
}
export function hasAttributes(ele, attrNames) {
if (typeof attrNames === 'string') {
return hasAttribute(ele, attrNames)
} else if (isArray(attrNames)) {
var result = false
for (var i = 0; i < attrNames.length; i++) {
var testResult = hasAttribute(ele, attrNames[i])
if (testResult) {
result = true
break
}
}
return result
}
}
export function throttle(func, wait, options) {
var context, args, result
var timeout = null
var previous = 0
if (!options) options = {}
var later = function() {
previous = options.leading === false ? 0 : now()
timeout = null
result = func.apply(context, args)
if (!timeout) context = args = null
}
return function() {
var nowtime = now()
if (!previous && options.leading === false) previous = nowtime
var remaining = wait - (nowtime - previous)
context = this
args = arguments
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout)
timeout = null
}
previous = nowtime
result = func.apply(context, args)
if (!timeout) context = args = null
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining)
}
return result
}
}
export function values(obj) {
var results = []
if (obj == null) {
return results
}
each(obj, function(value) {
results[results.length] = value
})
return results
}
export function toArray(iterable) {
if (!iterable) {
return []
}
if (iterable.toArray) {
return iterable.toArray()
}
if (isArray(iterable) || isArguments(iterable)) {
return Array.prototype.slice.call(iterable)
}
return values(iterable)
}
export function unique(arr) {
var temp
var n = []
var o = {}
for (var i = 0; i < arr.length; i++) {
temp = arr[i]
if (!(temp in o)) {
o[temp] = true
n.push(temp)
}
}
return n
}
export function check(p, onComplete) {
for (var i in p) {
if (Object.prototype.hasOwnProperty.call(p, i) && !checkOption.check(i, p[i], onComplete)) {
return false
}
}
return true
}
export function addPropsHook(data) {
var isNotProfileType = !data.type || data.type.slice(0, 7) !== 'profile'
var isSatisfy = sd.para.preset_properties && isNotProfileType
if (isSatisfy && sd.para.preset_properties.url && typeof data.properties.$url === 'undefined') {
data.properties.$url = getURL()
}
if (isSatisfy && sd.para.preset_properties.title && typeof data.properties.$title === 'undefined') {
data.properties.$title = document.title
}
}
export function coverExtend(obj) {
each(Array.prototype.slice.call(arguments, 1), function(source) {
for (var prop in source) {
if (source[prop] !== void 0 && obj[prop] === void 0) {
obj[prop] = source[prop]
}
}
})
return obj
}
目录在 utils/sdk
下面, 这是我简化后的目录结构
在线测试地址(简化后)
https://codesandbox.io/p/gith...
没看懂你想问啥。
ESM 的 import/export 都是 Top-Level 的,你可以理解为会提升(其实不是提升,但从现象上看跟提升的结果一样)。也就是说即便你 console.log 写在 import 前面,也是 import 先执行。