Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does prototyping Function not affect console.log?

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.

like image 362
YingYang Avatar asked Feb 05 '13 19:02

YingYang


People also ask

Why we should not use console log?

Untrustworthy Information You cannot always trust information reported by console. log() because there is simply no standardized behavior about it. You don't really know what happens under the hood. Most of the time, calling console.

Does prototype improve memory optimization?

'Prototype' helps remove code redundancy which helps boost your app's performance. If you are seeking to optimize resources or memory on your application, you should use prototype .

What is the difference between __ proto __ and prototype?

All the objects have proto property. The prototype gives access to the prototype of function using function. proto gives access to the prototype of the function using the object.

What is prototype in console log?

The prototype is an object that is associated with every functions and objects by default in JavaScript, where function's prototype property is accessible and modifiable and object's prototype property (aka attribute) is not visible.


1 Answers

This seems to be an issue with Firebug not with Firefox per se. My guess is that Function in Firebug lives in a different scope then Function in your page. (since unlike the other browsers Firebug is an extension , not a built in browser tool)

In fact if instead of Firebug you use the built in Firefox console (Ctrl+Shift+K), your code works perfectly fine.

More information about Firebug internals can be found here

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

This excerpt may be interesting

When Firebug is detached from Firefox, open in a new or separate window, the new window has its own scope. In that scope, a few Firebug script tags compile to create a connection back to the original browser.xul window. Most important, chrome.js is unique to each top level window, but the Firebug object used by the detached window is the object of the parent browser.xul.

like image 50
lostsource Avatar answered Oct 22 '22 19:10

lostsource