Giter VIP home page Giter VIP logo

Comments (5)

lcxfs1991 avatar lcxfs1991 commented on May 22, 2024

目前已经设置了no-var

from eslint-config-alloy.

mengjian-github avatar mengjian-github commented on May 22, 2024

@lcxfs1991 所以这项规则要打开嘛,保持**一致性~

from eslint-config-alloy.

xcatliu avatar xcatliu commented on May 22, 2024

当一个函数比较长时,var 都定义在最前面,而在后面才被赋值的话,阅读起来会比较麻烦——

  • 它是第一次被赋值吗?
  • 它是局部变量还是全局变量?
  • 它是函数参数传进来的吗?

from eslint-config-alloy.

mengjian-github avatar mengjian-github commented on May 22, 2024

@xcatliu 这条规则是用来限定var的,因为var后定义会进行变量提升,避免先使用后定义的情况。
不过对你说的这几个问题我有一些看法:

首先,一个函数比较长,就应该反思一下是否可以拆分为多个函数,往往一个长函数无法保证单一职责原则了。

其次,一个变量是局部变量还是全局变量,和定义的远近其实没有必然联系,一个变量往往有多处被使用的地方,并不能保证使用时一定能够在就近的地方定义,是否隐式定义全局变量要靠工具去检测,按照这个说法,我也可以说看到一个后面定义的变量,我还会担心这个变量到底是被定义过了还是没定义过呢?

最后,函数传参本身不需要定义,无论是前定义和后定义,而且这类查找应该借助的是编辑器的goto definition功能,而不是凭猜测来看是哪里定义的。

from eslint-config-alloy.

xcatliu avatar xcatliu commented on May 22, 2024

我们会要求一个函数尽量短,但是现实中有时确实很难做到,所以才没有限制一个函数的最大长度,只限制了复杂度,只要是线性的,太长也没关系。
看到一个后面定义的变量,不需要担心它在前面有没有被定义过,因为如果它在前面已经被定义过了,eslint 就会报错了。

from eslint-config-alloy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.