JavaScript でも、Ruby のように、より破壊的なメソッドの末尾に何か記号を付けたい
Qiita のエントリ を拝見して、
JavaScript も メソッド名には ! が使えないけど、$ は使えるので、Ruby の tr と tr! のように、
メソッド名から「より破壊的なメソッド(≒レシーバーの内容を変更するメソッド)」がわかると、どうなるのかな(良い作用があるかな)? と思い、以下のような I/F のコードを書いてみたり。
// --- class / interfaces ---------------------------------- function Foo(value) { // @arg String = "" - comment this._value = value || ""; } Foo["prototype"] = Object.create(Foo, { "constructor": { "value": Foo }, // new Foo(value:String = ""):Foo "concat": { "value": Foo_concat }, // Foo#concat(a:String):String "concat$": { "value": Foo_concat$ }, // Foo#concat$(a:String):this }); // --- implements ------------------------------------------ function Foo_concat(a) { // @arg String // @ret String return this._value + a; } function Foo_concat$(a) { // @arg String // @ret this this._value += a; return this; }
もちろん $ の仕様書上の意味(主に自動生成用だよ)や、単体で使用した場合のデファクトな意味(最近だとjQuery用かな)は理解した上で、ちょっと試してみようかなと。