6

I prototyped Function so that it has a getBody function:

Function.prototype.getBody = function() {
    // Get content between first { and last }
    var m = this.toString().match(/\{([\s\S]*)\}/m)[1];
    // Strip comments
    return m.replace(/^\s*\/\/.*$/mg,'');
};

See here for more info. I tried to test it this way:

console.log(console.log.getBody.getBody());

but received an error: TypeError: console.log.getBody is undefined. I figured out that maybe this happens because console.log was defined before I actually prototyped Function so I created an empty function x right before the prototyping and tried to call

console.log(x.getBody.getBody());

which worked without a problem. Checking the type of console.log with typeof console.log results in "function". Here's a CodePen to try it out. All of this wasn't really a surprise since it's what I expected except of console.log.getBody to be undefined.

So why does prototyping Function not affect console.log? I'm using Firefox 18.0.1 with Firebug 1.11.1.

4

1 回答 1

5

这似乎是 Firebug 的问题,而不是 Firefox 本身的问题。我的猜测是,Function在 Firebug 中与Function您的页面中的范围不同。(因为与其他浏览器不同,Firebug 是一个扩展,而不是内置的浏览器工具)

事实上,如果您使用内置的 Firefox 控制台 ( Ctrl+Shift+K ) 而不是 Firebug,那么您的代码可以正常工作。

关于 Firebug 内部的更多信息可以在这里找到

http://getfirebug.com/wiki/index.php/Firebug_Internals

这段摘录可能很有趣

当 Firebug 与 Firefox 分离时,在新窗口或单独窗口中打开,新窗口有自己的范围。在该范围内,一些 Firebug 脚本标记会编译以创建与原始 browser.xul 窗口的连接。最重要的是,chrome.js 对每个顶级窗口都是唯一的,但分离窗口使用的 Firebug 对象是父 browser.xul 的对象。

于 2013-02-05T19:45:03.260 回答