
今天偶尔翻到之前学 js 时候想要模仿 kotlin 的一些链调操作写的一个小玩具,外加上一些工具函数,在我用 nodejs 写的一些爬虫啥的小玩具里面用起来还挺方便,不过那时候学 js 不久,后面也没完善,给大家看个乐子吧
module.exports = { import(){ const functiOns={ randomStr(str='abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ',len){ const strLen=str.length let res='' for(i in this.range(0,len)){ res+=str[this.randomLen(strLen)] } return res }, range(start,end,step=1){ let arr=[] for(let i=start;i<=end;i+=step) arr.push(i) return arr }, randomIn(start,end){ return this.randomLen(end-start)+start }, randomLen(len){ return Math.floor(Math.random()*len) }, repeat(i,cb){this.range(1,i).forEach(cb())} } Object.assign(globalThis,functions) }, objEnhance(){ const functiOns={ pr() { console.log(this.valueOf()) return this.valueOf() }, also(cb){ let it=this.valueOf() cb(it) return it }, apply(cb){ cb(this) return this }, let(cb){ return cb(this.valueOf()) }, run(cb){ return cb(this) } } Object.assign(Object.prototype,functions) }, importAndEnhance(){ this.import() this.objEnhance() } } function test(){ const util=module.exports util.objEnhance() util.import() 'https://www.google.com/search?q=来点色图'.pr().let(encodeURI).pr().let(btoa).pr().let(atob).pr().let(decodeURI).pr() } test() 输出:
https://www.google.com/search?q=来点色图 https://www.google.com/search?q=%E6%9D%A5%E7%82%B9%E8%89%B2%E5%9B%BE aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS9zZWFyY2g/cT0lRTYlOUQlQTUlRTclODIlQjklRTglODklQjIlRTUlOUIlQkU= https://www.google.com/search?q=%E6%9D%A5%E7%82%B9%E8%89%B2%E5%9B%BE https://www.google.com/search?q=来点色图 1 tutou 2023-08-17 09:09:37 +08:00 这也不算原型链阿 |
4 Jamy 2023-08-17 11:36:08 +08:00 链式调用跟原型链有啥关系 |
5 heyline 2023-08-17 15:36:33 +08:00 |
7 ruoxie 2023-08-20 11:01:38 +08:00 |