console.log
私は最近、呼び出してfirebugsのコードを調べたところ、console.log.toString()
次のようになりました。
function () { return Function.apply.call(x.log, x, arguments); }
私が理解している限り、これによりが参照され、引数がおよびFunction.apply
で呼び出されます。それ自体が関数を呼び出すため、これはその参照とその引数として呼び出されることになります。this
x.log
x
arguments
Function.apply
x.log
this
x
arguments
これが私の質問につながります:Function.apply
を使用する代わりに、この方法で呼び出す理由はありますFunction.prototype.apply
か? つまり、上記との間に違いはありreturn x.log.apply(x, arguments)
ますか?
編集:オープンソースなので、firebugのソースコードをざっと見て、これが作成されている場所を見つけました(consoleInjector.js、73行目):
// Construct a script string that defines a function. This function returns
// an object that wraps every 'console' method. This function will be evaluated
// in a window content sandbox and return a wrapper for the 'console' object.
// Note that this wrapper appends an additional frame that shouldn't be displayed
// to the user.
var expr = "(function(x) { return {\n";
for (var p in console)
{
var func = console[p];
if (typeof(func) == "function")
{
expr += p + ": function() { return Function.apply.call(x." + p +
", x, arguments); },\n";
}
}
expr += "};})";
// Evaluate the function in the window sandbox/scope and execute. The return value
// is a wrapper for the 'console' object.
var sandbox = Cu.Sandbox(win);
var getConsoleWrapper = Cu.evalInSandbox(expr, sandbox);
win.wrappedJSObject.console = getConsoleWrapper(console);
これは、 pstFunction
の回答に対する最初のコメントで述べたように、別の範囲にあることと関係があるとほぼ確信していますが、まだ完全には理解していません。私はそれについてもう少し研究をするかもしれません。