LoginSignup
1
1

More than 5 years have passed since last update.

非推奨のメソッドを使用した時にメッセージを表示させる

Posted at

いずれ不要になる(非推奨)メソッドを使用したときにメッセージを出力させる関数を作成した。

非推奨メソッドを使用したときにメッセージを表示させる関数

CoffeeScript
deprecation = (out) -> () => 
   switch arguments.length
     when 3 then [thiz, name, f] = arguments
     when 2 then [name, f] = arguments
     else throw new ArgumentException("argument length error!")
   that = if (thiz) then thiz else this
   that[name] = () -> 
        out("*** WARNING *** deprecated function:#{name}")
        f.apply(this, arguments)

関数の使用する前に、共通的な出力先を指定する。
非推奨メッセージの出力先を、都度変える場合は不要。

CoffeeScript
#deprecated = deprecation(alert) #alertを出したい場合
deprecated = deprecation(console.log) # console.logを出したい場合

この関数は次のように使用する。

deprecated <クラスメソッドの場合は'@,‘そうでない場合は指定しない> "メソッド名", メソッド本体

このようなメソッドがあれば

CoffeeScript
@fnca = (a) -> console.log(a)
fncb = (a) -> console.log(a)

こうなる。

CoffeeScript
deprecated @,"fnca", (a) -> console.log(a)
deprecated "fncb", (a) -> console.log(a)

実行結果(非推奨前)

a
b

実行結果(非推奨後)

"*** WARNING *** deprecated function:fnca
a
"*** WARNING *** deprecated function:fncb
b

クラス内のメソッドも同様に使用可能

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1