JavaScript中应避免的事情(不良部分)

  • Avoid creating a new object by using new Object(). Use the object literal syntax {} instead.

    避免通过使用new Object()创建新对象。 请改用对象文字语法{}

  • Same thing for arrays, favor [] over new Array().

    数组也一样,相对于new Array()更喜欢[]

  • Avoid blocks except where statements require them (if, switch, loops, try).

    避免使用块,除非语句需要它们( ifswitch ,循环, try )。

  • Never assign inside an if of while statements condition part

    切勿在if of while语句条件部分内分配

  • Never use == and !=. Use === and !== instead.

    切勿使用==!= 。 使用===!==代替。

  • Never use eval. Why? It has performance issues (it runs the interpreter/compiler), it has security issues (code injection if used with user input), difficulties in debugging.

    永远不要使用eval 。 为什么? 它存在性能问题(运行解释器/编译器),存在安全性问题(如果与用户输入一起使用则注入代码),调试困难。

  • Never use with, as it modifies the scope chain and can be a source of confusion.

    切勿with使用,因为它会修改范围链,并可能引起混乱。

  • Always pass functions to setTimeout and setInterval

    始终将函数传递给setTimeoutsetInterval

  • Never use Array as an associative arrays, use Object instead. The part of the Array object that provides that functionality is in fact provided by the Object prototype, so you could really have used a Date object for that same thing.

    切勿将Array用作关联数组,而应使用Object 。 实际上,提供对象功能的Array对象部分是由Object原型提供的,因此您实际上可以将Date对象用于同一件事。

  • Don’t use \ at the end of a string to create a multiline string, it’s not part of ECMAScript. Use string concatenation ' string1 ' + ' string2 ' instead

    不要在字符串的末尾使用\来创建多行字符串,因为它不是ECMAScript的一部分。 使用字符串串联' string1 ' + ' string2 '

  • Never modify the prototypes of the built-in objects Object and Array. Modify other prototypes of other objects such as Function with caution as it could lead to bugs hard to debug.

    切勿修改内置对象ObjectArray的原型。 请谨慎修改其他对象(例如Function其他原型,因为这可能会导致难以调试的错误。

翻译自: https://flaviocopes.com/things-to-avoid-in-javascript-bad-parts/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值