这是浏览器的 bug 还是 feature,我的 this 呢? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
请不要在回答技术问题时复制粘贴 AI 生成的内容
ssshooter
V2EX    程序员

这是浏览器的 bug 还是 feature,我的 this 呢?

  •  
  •   ssshooter
    ssshooter 2021-11-15 18:09:59 +08:00 2844 次点击
    这是一个创建于 1424 天前的主题,其中的信息可能已经有所发展或是发生改变。

    MJF5~FMPX_42@_S_IGGL_C3.png

    12 条回复    2021-11-15 22:22:29 +08:00
    cxe2v
        1
    cxe2v  
       2021-11-15 18:21:04 +08:00
    箭头函数内的 this 有它自己的想法

    一般来说是继承上级词法作用域的 this ,这里看来是继承了 map 这个 function 的 this
    ssshooter
        2
    ssshooter  
    OP
       2021-11-15 18:28:44 +08:00
    qiguai2017
        3
    qiguai2017  
       2021-11-15 19:22:28 +08:00
    箭头函数没有 this 变量
    hgc81538
        5
    hgc81538  
       2021-11-15 19:49:56 +08:00
    var self = this;

    (function(){
    console.log(this, self);
    })()
    forgcode
        6
    forgcode  
       2021-11-15 19:50:34 +08:00
    为啥要用 eval 而不直接 this.valuesJson[question.variable]。箭头函数会被编译吧,编译之后是临时变量就是不 this 了,写在 eval 中的 this 不会被编译到
    codehz
        7
    codehz  
       2021-11-15 20:22:32 +08:00
    原因是被编译( ES6->ES5 )成
    var _this = this
    ... map(function (question) {
    eval('this.xxx' + xxx)
    });
    这样了。。。
    显而易见编译不可能处理 eval 里面的东西(
    kid740246048
        8
    kid740246048  
       2021-11-15 20:23:46 +08:00
    本质原因是 eval 的调用者是 window ,你开个 F12 跑下这行代码就知道了
    `eval("console.log(this)")`
    cheese
        9
    cheese  
       2021-11-15 20:28:29 +08:00
    原因楼上说了,但是我想知道这里为啥要 eval 啊?
    ssshooter
        10
    ssshooter  
    OP
       2021-11-15 21:47:55 +08:00
    @cheese 因为懒得写个 while ,variable 可能是 `a.b.c.d`
    ssshooter
        11
    ssshooter  
    OP
       2021-11-15 21:48:30 +08:00
    @forgcode
    @codehz 感谢
    cheese
        12
    cheese  
       2021-11-15 22:22:29 +08:00
    @ssshooter #10 对象的深层属性获取,可以看下 lodash 的_.get 还挺好用的 https://lodash.com/docs/#get
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     923 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 29ms UTC 19:19 PVG 03:19 LAX 12:19 JFK 15:19
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86