Welcome to visit personal blog website: www.zhuxin.club

1. No essential difference

For objects such as array, object, function, and regular expressions that can be declared either by literals (such as let arr = []) or by constructors (such as let arr-new array ()), the effect of both declarations is essentially the same as the underlying implementation. The values created are wrapped by wrapping objects.

2. Avoid constructors

Never declare a variable with a constructed rain number if you don’t have to, it often has some drawbacks:

  • New Array() if there is only one numeric parameter, that parameter is treated as the length value of the Array, rather than an element of the Array(as in new Array(3)), and an Array of three “empty cells” is created
  • When creating an Object, you cannot set multiple properties at once, as literals do, but only one by one
  • You rarely define functions by new Function()
  • Regular expressions created using literal forms (such as /~a*bt/g) have simpler syntax and are more efficient to execute than constructors (js will precompile and cache them before execution).