T
Thomas 'PointedEars' Lahn
sasuke said:Thomas said:Date.prototype.fmt = function(format) {
var d = this;
return format.replace(/%([CdFfHMmnRSTYy%])/g,
function(m, p1) {
[...]
});
};
Very good modifications. But won't it be better if we use a cached
copy of the RegExp native object instead of creating a new one on each
invocation?
It would, if what you describe were the case. With a RegExp literal
no new object is created, but an already existing object is being
referred to. So there is no need for optimization through "caching":
,-[ECMAScript Language Specification, Edition 3 Final]
|
| 7 Lexical Conventions
|
| The source text of an ECMAScript program is first converted into
| a sequence of input elements, which are either tokens, line
terminators,
| comments, or white space. The source text is scanned from left to
right,
| repeatedly taking the longest possible sequence of characters as
| the next input element.
|
| [...]
|
| 7.8.5 Regular Expression Literals
|
| A regular expression literal is an input element that is converted
to
| a RegExp object (section 15.10) when it is scanned. The object is
| created before evaluation of the containing program or function
begins.
| Evaluation of the literal produces a reference to that object; it
does
| not create a new object.
|
| [...]
| Semantics
|
| A regular expression literal stands for a value of the Object type.
| This value is determined in two steps: first, the characters
comprising
| the regular expression's RegularExpressionBody and
RegularExpressionFlags
| production expansions are collected uninterpreted into two strings
Pattern
| and Flags, respectively. Then the new RegExp constructor is called
with
| two arguments Pattern and Flags and the result becomes the value of
the
| RegularExpressionLiteral. If the call to new RegExp generates an
error,
| an implementation may, at its discretion, either report the error
| immediately while scanning the program, or it may defer the error
until
| the regular expression literal is evaluated in the course of program
| execution.
PointedEars