首页 > 网站开发 > JavaScript >
-
JavaScript教程之es7你都懂了吗?今天带你了解es7的(2)
target[symbol] = false;
/**
* [description]
* @param {[Object]} params={} [请求参数]
* @param {[Number]} startNum=null [手动重置加载页数]
* @param {[Boolean]} clearlist=false [是否清空数组]
* @return {[Object]} [{所有加载页数组集合,加载完成状态}]
*/
descriptor.value = async function(params={},startNum=null,clearlist=false) {
try {
if (target[symbol]) return;
// 函数执行前赋值操作
target[symbol] = true;
params.data.pageNum = pageNum;
if (startNum !== null && typeof startNum === 'number') {
params.data.pageNum = startNum;
pageNum = startNum;
}
if (clearlist) oldList = [];
// 释放函数,取回list
let before = current;
current = await oldFunc.call(this,params);
// 函数执行结束赋值操作
(state === 'hasmore' || clearlist) && oldList.push(...current);
if ((current.length === 0) || (params.data.pageSize > current.length)) {
needToast && this.$toast({title: '没有更多了',type: 'fail'});
state = 'nomore';
} else {
state = 'hasmore';
pageNum++;
}
target[symbol] = false;
this.$apply();
return { list : oldList,state };
} catch(e) {
console.error('fail code at: ' + e)
}
}
}
}
/**
* 检测工具
*/
const _toString = Object.prototype.toString;
// 检测是否为纯粹的对象
const _isPlainObject = function (obj) {
return _toString.call(obj) === '[object Object]'
}
// 检测是否为正则
const _isRegExp = function (v) {
return _toString.call(v) === '[object RegExp]'
}
/**
* @description 检测函数
* 用于检测类型action
* @param {Array} checked 被检测数组
* @param {Array} checker 检测数组
* @return {Boolean} 是否通过检测
*/
const _check = function (checked,checker) {
check:
for(let i = 0; i < checked.length; i++) {
if(/(any)/ig.test(checker[i]))
continue check;
if(_isPlainObject(checked[i]) && /(object)/ig.test(checker[i]))
continue check;
if(_isRegExp(checked[i]) && /(regexp)/ig.test(checker[i]))
continue check;
if(Array.isArray(checked[i]) && /(array)/ig.test(checker[i]))
continue check;
let type = typeof checked[i];
let checkReg = new RegExp(type,'ig')
if(!checkReg.test(checker[i])) {
console.error(checked[i] + 'is not a ' + checker[i]);
return false;
}
}
return true;
}
/**
* @description 检测类型
* 1.用于校检函数参数的类型,如果类型错误,会打印错误并不再执行该函数;
* 2.类型检测忽略大小写,如string和String都可以识别为字符串类型;
* 3.增加any类型,表示任何类型均可检测通过;
* 4.可检测多个类型,如 "number array",两者均可检测通过。正则检测忽略连接符 ;
*/
export function typeCheck() {
const checker = Array.prototype.slice.apply(arguments);
return function (target, funcName, descriptor) {
let oriFunc = descriptor.value;
descriptor.value = function () {
let checked = Array.prototype.slice.apply(arguments);
let result = undefined;
if(_check(checked,checker) ){
result = oriFunc.call(this,...arguments);
}
return result;
}
}
};
const errorLog = (text) => {
console.error(text);
return true;
}
/**
* @description 全埋点
* 1.在所有methods方法中埋点为函数名
* 2.在钩子函数中'beforeCreate','created','beforeMount','mounted','beforeUpdate','activated','deactivated'依次寻找这些钩子
* 如果存在就会增加埋点 VIEW
*
* 用法:
* @Buried
* 在单文件导出对象一级子对象下;
* 如果某方法不想设置埋点 可以 return 'noBuried' 即可
*/
export function Buried(target, funcName, descriptor) {
let oriMethods = Object.assign({},target.methods),
oriTarget = Object.assign({},target);
// methods方法中
if(target.methods) {
for(let name in target.methods) {
target.methods[name] = function () {
let result = oriMethods[name].call(this,...arguments);
// 如果方法中返回 noBuried 则不添加埋点
if(typeof result === 'string' && result.includes('noBuried')) {
console.log(name + '方法设置不添加埋点');
} else if(result instanceof Promise) {
result.then(res => {
if(typeof res === 'string' && res.includes('noBuried')) { console.log(name + '方法设置不添加埋点'); return; };
console.log('添加埋点在methods方法中:' , name.toUpperCase ());
this.$log(name);
});
}else{
console.log('添加埋点在methods方法中:' , name.toUpperCase ());
this.$log(name);
};
return result;
}
}
}
// 钩子函数中
const hookFun = (hookName) => {
target[hookName] = function() {
let result = oriTarget[hookName].call(this,...arguments);
console.log('添加埋点,在钩子函数' + hookName + '中:', 'VIEW');
this.$log('VIEW');
return result;
}
}
const LIFECYCLE_HOOKS = [
'beforeCreate',
'created',
'beforeMount',
'mounted',
'beforeUpdate',
'activated',
'deactivated',
];
for(let item of LIFECYCLE_HOOKS) {
if (target[item]) return hookFun(item);
}
}