-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ecec184
commit 3ccdc8d
Showing
1 changed file
with
6 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
# Declaring instances | ||
|
||
Doesn't matter if you use **constructor** | ||
When declaring a `RegExp`, it oesn't matter if you use a **constructor**: | ||
|
||
```js | ||
const reUsername = /^[a-zA-Z0-9]+$/g | ||
``` | ||
|
||
or **expression literal** | ||
or if you use an **expression literal**: | ||
|
||
```js | ||
const reUsername = RegExp('^[a-zA-Z0-9]+$', 'g') | ||
``` | ||
|
||
The important thing is store it into a variable to make it reusable. | ||
The important thing is to store it into a variable to make it reusable. | ||
|
||
As you can see I don't use `new` because it is unnecessary. According with [ECMAScript](http://www.ecma-international.org/ecma-262/6.0/#sec-regexp-constructor): | ||
The use of `new` is unnecessary. According to [ECMAScript](http://www.ecma-international.org/ecma-262/6.0/#sec-regexp-constructor): | ||
|
||
> The RegExp constructor is the %RegExp% intrinsic object and the initial value of the RegExp property of the global object. When RegExp is called as a function rather than as a constructor, it creates and initializes a new RegExp object. Thus the function call RegExp(…) is equivalent to the object creation expression new RegExp(…) with the same arguments. | ||
> The RegExp constructor is the %RegExp% intrinsic object and the initial value of the RegExp property of the global object. When RegExp is called as a function rather than as a constructor, it creates and initializes a new RegExp object. Thus the function call RegExp(...) is equivalent to the object creation expression new RegExp(...) with the same arguments. | ||
*Another reason for hate `new`, right?* | ||
*Another reason for hating `new`, right?* |