跳至主要內容

Question

小于 1 分钟

Question

地址变化

onbeforeunloadopen in new window
onpopstateopen in new window
onhashchangeopen in new window
pushStateopen in new window
replaceStateopen in new window

判断数据类型的方法

typeofopen in new window

执行字符串代码

@typescript-eslint/no-implied-evalopen in new window

监听网络请求

VConsole源码分析与思考の初始化open in new window
vConsole fetch.proxy.tsopen in new window
vConsole xhr.proxy.tsopen in new window

/**
 * @param {string} fullPath
 * @returns {boolean}
 */
function existsFullPathProperty(fullPath) {
  const parts = fullPath.split('.');
  const objName = parts.shift();
  let obj; try { obj = eval(objName); } catch (e) {
    if (e instanceof ReferenceError) return false; else throw e;
  }
  if (typeof obj === 'undefined' || obj === null) return false;
  return existsObjectPartsProperty(obj, parts);
}

/**
 * @param {string} objName
 * @param {string} path
 * @returns {boolean}
 */
function existsNamePathProperty(objName, path) {
  let obj; try { obj = eval(objName); } catch (e) {
    if (e instanceof ReferenceError) return false; else throw e;
  }
  if (typeof obj === 'undefined' || obj === null) return false;
  const parts = path.split('.');
  return existsObjectPartsProperty(obj, parts)
}

/**
 * @param {string} objName
 * @param {string} path
 * @returns {boolean}
 */
function existsObjectPathProperty(obj, path) {
  if (typeof obj === 'undefined' || obj === null) return false;
  const parts = path.split('.');
  return existsObjectPartsProperty(obj, parts)
}

/**
 * @param {any} obj
 * @param {string[]} parts
 * @returns {boolean}
 */
function existsObjectPartsProperty(obj, parts) {
  let current = obj;
  for (let i = 0; i < parts.length; i++) {
    if (typeof current[parts[i]] === 'undefined') return false;
    current = current[parts[i]];
  }
  return true;
}