标签:函数 console rap null actor == throw name struct
<script> function isFunction(arg) { return typeof arg === "function"; } function isGeneratorFunction(obj) { return obj && obj.constructor && "GeneratorFunction" === obj.constructor.name; } function isAsyncFunction(obj) { return obj && obj.constructor && "AsyncFunction" === obj.constructor.name; } function isPromise(obj) { return obj && "function" === typeof obj.then; } /** * 切面编程 * @param {Function | GeneratorFunction | AsyncFunction} fn - 方法 * @param {*} beforeFn - 前置方法 * @param {*} afterFn - 后置方法 */ function aop(fn, beforeFn, afterFn) { if (isGeneratorFunction(fn)) { console.log(`isGeneratorFunction`) return function (...args) { if (isFunction(beforeFn)) { beforeFn.apply(this, args); } let ret = fn.apply(this, args); let next = ret.next; ret.next = function(...arg){ const done = next.apply(ret, arg); console.log(done); if(done.done) {debugger if (isFunction(afterFn)) { afterFn.apply(this, args); } } return done } return ret; }; } if (isAsyncFunction(fn)) { return async function(...args) { if (isFunction(beforeFn)) { beforeFn.apply(this, args); } return await fn.apply(this, args).then((data) => { if (isFunction(afterFn)) { afterFn.apply(this, args); } return data }) .catch((err) => { if (isFunction(afterFn)) { afterFn.apply(this, args); } throw err; }); }; } if (isFunction(fn)) {console.log(1111) return function(...args) { if (isFunction(beforeFn)) { beforeFn.apply(this, args); } const ret = fn.apply(this, args); if (isFunction(afterFn)) { if (isPromise(ret)) { ret .then(() => { afterFn.apply(this, args); }) .catch(() => { afterFn.apply(this, args); }); } else { afterFn.apply(this, args); } } return ret; }; } } function before(fn, callback) { return aop(fn, callback, null); } function after(fn, callback) { return aop(fn, null, callback); } function wrap(fn, beforeFn, afterFn) { return aop(fn, beforeFn, afterFn); } function factoryAop(fn) { //非函数或者已有aop方法原样输出 if (typeof fn !== "function" || fn.before || fn.after || fn.wrap) { return fn; } fn.before = function(callback) { const me = this; return factoryAop(before(me, callback)); }; fn.after = function(callback) { const me = this; return factoryAop(after(me, callback)); }; fn.wrap = function(beforeFn, afterFn) { const me = this; return factoryAop(wrap(me, beforeFn, afterFn)); }; return fn; } function testFn() { console.log(‘hello test‘); } function beforeTestFn(){ console.log(‘before test fn‘); } function afterTestFn(){ console.log(‘after test fn‘); } //const proxyTestFn = factoryAop(testFn) // const fn = proxyTestFn.before(beforeTestFn).after(afterTestFn); // fn(); // const fn2 = proxyTestFn.wrap(beforeTestFn, afterTestFn); // fn2(); // const asyncFn = async function() { // return await new Promise((resolve, reject) => { // setTimeout(() => { // resolve(1000); // }, 1000) // }) // } // const proxyTestFn = factoryAop(asyncFn) // const fn = proxyTestFn.before(beforeTestFn).after(afterTestFn); // fn() // .then((data)=> { // console.log(`成功!!`) // console.log(data); // }) // .catch((data) => { // alert(`失败1`); // console.log(data); // }) // const fn2 = proxyTestFn.wrap(beforeTestFn, afterTestFn); // fn2(); // const promiseFn = function() { // return new Promise((resolve, reject) => { // setTimeout(() => { // reject(1000); // }, 1000) // }) // } // const proxyTestFn = factoryAop(promiseFn) // const fn = proxyTestFn.before(beforeTestFn).after(afterTestFn); // fn() // .then((data)=> { // console.log(`成功!!`) // console.log(data); // }) // .catch((data) => { // alert(`失败1`); // console.log(data); // }) let generatorFn = function*(x) { console.log(‘x‘, x); let a = yield x; console.log(‘xx‘, x); console.log(‘a‘, a); let b = yield (x + 1) + a; yield a + b; console.log(‘a + b =‘, a + b); return a + b; } // let pg = generatorFn(10); //let ret1 = pg.next(); const proxyTestFn = factoryAop(generatorFn) const fn = proxyTestFn.after(afterTestFn); let g = fn(10); let ret = g.next(); //let g = proxyTestFn(10); //let ret = g.next(); console.log(ret); </script>
标签:函数 console rap null actor == throw name struct
原文地址:https://www.cnblogs.com/hellolol/p/11659921.html