LoginSignup
6
6

More than 5 years have passed since last update.

console.logをラップしたい

Posted at

可変長引数の関数をラップするような関数を作るときにapplyってのを使って次のようなことしたりするらしい。(なんだかJavaScriptって可変長引数扱いやすいんだか扱いにくいんだか良くわからんです。。。)

function wrapperfunc() {
    // something to do before calling func
    ;

    // call func
    func.apply(this, arguments);

    // something to do after calling func
    ;
}

でも、funcがconsole.logだとIllegal Invocationとか言われちゃうんだよね。

ということでグーグル先生に教えてもらいました。
http://stackoverflow.com/questions/8159233/typeerror-illegal-invocation-on-console-log-apply

console.ほにゃらら のthisはconsoleじゃないといけないらしいです。

なので、

    console.log.apply(console, arguments);

としなければならないということだそうです。

6
6
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
6
6