ぶれすとつーる

だいたいjavascript

正規表現リテラルのes3からes5の間での変化

f:id:nazomikan:20140312013303j:plain

何度目かの聖書(JavaScript: The Good Parts)の輪読会をしてたときに、RegExpの章を担当してくれた子が、「正規表現リテラルから生成される正規表現オブジェクトは内容が同じなら参照も同じになります!」って説明してくれて、「あれ、そうだっけ」って思って調べた。

たしかに書いてる

RegExp objects made by regular expression literals share a single instance:
function make_a_matcher() {
    return /a/gi;
}
var x = make_a_matcher();
var y = make_a_matcher();
// Beware: x and y are the same object!
x.lastIndex = 10;
document.writeln(y.lastIndex);    // 10
via JavaScript: The Good Parts

半信半疑で実行するとやっぱりそんなことなかった

f:id:nazomikan:20140312013722p:plain

ということでどこかで仕様かわったんだろうなーって思ってみてたら3rdから5thの流れの中でそれらしい変更を見つけた

3rd

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. Two regular expression literals in a program evaluate to regular expression objects that never compare as === to each other even if the two literals' contents are identical. A RegExp object may also be created at runtime by new RegExp (section 15.10.4) or calling the RegExp constructor as a function (section 15.10.3).

5th

Regular Expression Literals

A regular expression literal is an input element that is converted to a RegExp object (see 15.10) each time the literal is evaluated. Two regular expression literals in a program evaluate to regular expression objects that never compare as === to each other even if the two literals' contents are identical. A RegExp object may also be created at runtime by new RegExp (see 15.10.4) or calling the RegExp constructor as a function (15.10.3).

後者の一文がなくなったってことは都度評価されて都度新しいオブジェクトが返却されてるんでしょうかね(返却される正規表現オブジェクト自体は内容が同じでも別物になるって次の文にかいてある)

正規表現リテラルもいろいろかわってたんだなー(遠い目

なるほどー。