ram
6/18/2014 12:26:00 AM
Cezary Tomczyk <cezary.tomczyk@gmail.com> writes:
>Subject: Bind custom function to the all Function objects
I read »to the all Function objects«
as »to all the function objects«.
>I am looking for a solution where I can bind custom function to the all
>Function objects. Whenever any function/method will be called I'd like
>to call my custom function. I am going to use it only for
>testing/debugging. Doesn't have to be cross-browser.
>Can someone suggest from where to start?
The program below calls
instrument( Object.getPrototypeOf( global.document ))
to add a call
global.console.log( "function " + key + " was called" );
in front of every defined function of the document
prototype.
When the document.writeln( 2 ); is then executed at the end,
the text
"function writeln was called"
is being written to the console.
Main.html =
<!DOCTYPE HTML><html><head><meta charset="UTF-8">
<title>Main</title><link rel="stylesheet" href="main.css">
</head><body><pre><code>
<script type="application/javascript;version=1.7" src="main.js"></script>
</code></pre></body></html>
main.css =
main.js =
"use strict";
const global = this;
const all = global;
function main()
{ "use strict";
const seen = global.Object.create( null );
const unseen = function( container, key, object )
{ "use strict";
if( typeof object === "function" )
{ let old = object;
container[ key ]= function()
{ global.console.log( "function " + key + " was called" );
return old.apply( this, arguments ); }}
else if( typeof object === "object" )
{ /* enumerate( object ); */ }
seen[ object ]= true; }
const enumerate = function( object )
{ "use strict";
if( typeof global.Object.getOwnPropertyNames === "function" )
{ const key = global.Object.getOwnPropertyNames( object );
const keyLength = key.length;
for( let i = 0; i < keyLength; ++i )
{ try
{ let entity = object[ key[ i ]];
if( !seen[ entity ])unseen( object, key[ i ], entity ); }
catch( exception ){} }}}
const instrument = function( object )
{ "use strict"; enumerate( object ); }
seen[ global.Object.getOwnPropertyNames ]= true;
instrument( Object.getPrototypeOf( global.document )); }
main();
document.writeln( 2 );