问题
What is the difference between RegExp('hi')
and new RegExp('hi')
?
Does the new
keyword do anything here?
回答1:
It is identical
The
RegExp
constructor is the%RegExp%
intrinsic object and the initial value of theRegExp
property of the global object. WhenRegExp
is called as a function rather than as a constructor, it creates and initializes a newRegExp
object. Thus the function callRegExp(…)
is equivalent to the object creation expressionnew RegExp(…)
with the same arguments.
From http://www.ecma-international.org/ecma-262/6.0/#sec-regexp-constructor
来源:https://stackoverflow.com/questions/39215710/javascript-new-regexphi-versus-regexphi