6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?