V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
cuberwr1
V2EX  ›  分享创造

js 使用原型链表演杂技

  •  
  •   cuberwr1 · 255 天前 · 1415 次点击
    这是一个创建于 255 天前的主题,其中的信息可能已经有所发展或是发生改变。

    今天偶尔翻到之前学 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 条附言  ·  255 天前
    https://github.com/thedaviddelta/scope-extensions-js
    楼下有位老哥发了这个,很好用
    7 条回复    2023-08-20 11:01:38 +08:00
    tutou
        1
    tutou  
       255 天前
    这也不算原型链阿
    cuberwr1
        2
    cuberwr1  
    OP
       255 天前 via Android
    @tutou string 上找不到的方法往上找 object ,这不是用了原型链吗
    tutou
        3
    tutou  
       255 天前
    @cuberwr1 我眼瞎了,没看完。TVT
    Jamy
        4
    Jamy  
       255 天前
    链式调用跟原型链有啥关系
    heyline
        5
    heyline  
       255 天前
    cuberwr1
        6
    cuberwr1  
    OP
       255 天前
    @heyline #5 可以,很好,直接 npm 就能用,之后就用这个了
    ruoxie
        7
    ruoxie  
       252 天前
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3268 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 11:43 · PVG 19:43 · LAX 04:43 · JFK 07:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.