• 请不要在回答技术问题时复制粘贴 AI 生成的内容
ye22st
V2EX  ›  程序员

在 vue 里面到底哪种写法比较规范?

  •  
  •   ye22st · Jun 27, 2020 · 5758 views
    This topic created in 2322 days ago, the information mentioned may be changed or developed.

    我看到很多项目里面对于 this 指向的写法都是 let self = this; 有些人跟我说就这样写,有些人又说这种写法不规范? 请教一下 V 站的大佬,哪种比较规范?

    43 replies  •  2020-06-29 10:10:17 +08:00
    nianyu
    nianyu
    Jun 27, 2020❤️1
    1
    let self = this 没问题,以前很多库的源码都是这么写的,这东西无所谓的
    ujued
    ujued
    Jun 27, 2020 via iPhone❤️1
    2
    let forClosure = this
    loading
    loading
    Jun 27, 2020 via Android❤️1
    3
    let that=this
    或者
    let _this=this
    sagaxu
    sagaxu
    Jun 27, 2020 via Android❤️1
    4
    写 const 也行
    Biwood
    Biwood
    Jun 27, 2020❤️11
    5
    既然已经用 let 关键字了,那么完全可以用箭头函数来规避这种语法,我反正是从写 ES6 开始很久都没这么写了,除非你有什么特殊需求?
    Hanggi
    Hanggi
    Jun 27, 2020❤️5
    6
    这个问题上次不是都说了嘛:


    const 这个 = this;

    这个.submit();
    qyc666
    qyc666
    Jun 27, 2020 via iPhone❤️1
    7
    为什么不用箭头函数
    seeker
    seeker
    Jun 27, 2020❤️1
    8
    哪那么多讲究的,我都是 `const this2 = this`
    ChanKc
    ChanKc
    Jun 27, 2020❤️1
    9
    箭头函数,self,that 都可以
    xxx749
    xxx749
    Jun 27, 2020 via Android❤️1
    10
    const context = this
    Trim21
    Trim21
    Jun 27, 2020 via Android❤️1
    11
    我之前看人用的 vm
    zhuangzhuang1988
    zhuangzhuang1988
    Jun 27, 2020❤️1
    12
    自己怎么爽怎么 来..
    ChanKc
    ChanKc
    Jun 27, 2020❤️2
    13
    @Trim21 vm 就只在 Vue 的场景下比较合适,没有 that 或者 self 通用
    ye22st
    ye22st
    OP
    Jun 27, 2020
    14
    好的,谢谢各位大佬解答。
    BXGo
    BXGo
    Jun 27, 2020 via Android
    15
    有文档规范
    mxT52CRuqR6o5
    mxT52CRuqR6o5
    Jun 27, 2020 via Android
    16
    没有那种最规范,看团队怎么规定
    SilentDepth
    SilentDepth
    Jun 27, 2020
    17
    没见过有规范明确要求这样处理 this 。只要不产生歧义,并且你(和你的协作者)清楚 this 在代码中的实际指向,怎么写着爽怎么来。
    surfwave
    surfwave
    Jun 27, 2020
    18
    用箭头函数啊
    Lxxyx
    Lxxyx
    Jun 27, 2020
    19
    一直用的 ctx 。

    ```js
    const ctx = this;
    ```
    ppgs8903
    ppgs8903
    Jun 27, 2020
    20
    let that = this
    其他都容易冲突,self 用在 类 CLASS 的方案里面,不建议
    ChanKc
    ChanKc
    Jun 27, 2020
    21
    @ppgs8903 这个怎么说?
    zhuisui
    zhuisui
    Jun 27, 2020❤️1
    22
    @ChanKc 比如 self 在 webpack 被导出作为 this 的别名是很常见的,所以认为它应该是 window 的别名
    Vegetable
    Vegetable
    Jun 27, 2020
    23
    变量名还有啥规范,纯粹一点好吗,何况现在有箭头函数
    wish8023
    wish8023
    Jun 27, 2020 via Android
    25
    建议用 ES6 语法,在现代浏览器,基本都可用了。
    WilliamLin
    WilliamLin
    Jun 28, 2020
    26
    let _this = this
    hitaoguo
    hitaoguo
    Jun 28, 2020
    27
    能不要新定义个变量就不要定义,这样能加深你对 this 指向的理解。
    大部分情况下用箭头函数就能解决。
    除非说在函数里面有它自己的 this 需要用到,同时还需要外部的 this,那么写 vue 的话我一般是 let vm = this 。
    gitjavascript
    gitjavascript
    Jun 28, 2020
    28
    反正 let 肯定是不好的
    guanhui07
    guanhui07
    Jun 28, 2020
    29
    _this 或
    that 用的人比较多
    cz5424
    cz5424
    Jun 28, 2020
    30
    建议改成 ES6,不用 this 。this 真烦
    TomatoYuyuko
    TomatoYuyuko
    Jun 28, 2020
    31
    见过用 entity = this 的也不错,
    Junh
    Junh
    Jun 28, 2020 via iPhone
    32
    这好像和 vue 没什么关系吧
    optional
    optional
    Jun 28, 2020
    33
    为啥是 let ? 这里显然应该用 const
    `
    const self = this
    const $this = this
    const _this = this
    ·
    Tdy95
    Tdy95
    Jun 28, 2020
    34
    业务代码里面使用箭头函数,保证 this 指向不丢失即可。

    vue 的生命周期等方法调用的时候都帮你把 this 实例注入了好了
    wobuhuicode
    wobuhuicode
    Jun 28, 2020❤️2
    35
    其实写习惯了 C 系列语言都喜欢 let self = this
    写习惯 java 的估计就喜欢 let that = this
    写习惯前端的都喜欢用箭头函数。
    xiangyuecn
    xiangyuecn
    Jun 28, 2020
    36
    This
    Martox
    Martox
    Jun 28, 2020
    37
    let that = this
    soulmt
    soulmt
    Jun 28, 2020
    38
    我就觉得很 low 多此一举不是么
    soulmt
    soulmt
    Jun 28, 2020
    39
    @hitaoguo 对,如果不是为了区分 2 个 this 不一致, 完全没必要申明一个新的;
    sunwang
    sunwang
    Jun 28, 2020
    40
    有了箭头函数就没用过这个写法了
    kinghly
    kinghly
    Jun 28, 2020 via Android
    41
    先要明白为什么这么写,可以看下 js 闭包。let 、const 、()=>{}不是万能的。
    ghosthcp516
    ghosthcp516
    Jun 28, 2020
    42
    要么用箭头函数,如果要写兼容代码请用 var self = this,你这个等于是缝合怪写法
    ryanlid
    ryanlid
    Jun 29, 2020
    43
    在浏览器中,self 是有值的,指向当前 window 对象的引用。

    https://developer.mozilla.org/zh-CN/docs/Web/API/Window/self
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2589 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 88ms · UTC 00:28 · PVG 08:28 · LAX 16:28 · JFK 19:28
    ♥ Do have faith in what you're doing.