Skip to content

Instantly share code, notes, and snippets.

@puppybits
Created July 19, 2013 02:36
Show Gist options
  • Save puppybits/6034692 to your computer and use it in GitHub Desktop.
Save puppybits/6034692 to your computer and use it in GitHub Desktop.
inspect and log random objects, functions and json
radioactivate = function(obj)
{
for (var attr in obj)
{
if (typeof obj[attr] === "function")
(function(obj, attr){
var _attr = obj[attr];
obj[attr] = function(){
var input = Array.prototype.slice.call(arguments,0);
var output = _attr.apply(_attr, arguments)
console.log("%c> %c"+attr+"%c( "+input+" ) %c"+output, 'color:lightgrey', 'color:red', 'color:black', 'color:blue')
return output;
}
}(obj, attr))
else if (typeof obj[attr] === "object")
(function(obj, attr){
var _attr = obj[attr];
Object.defineProperty(obj, attr,
{set : function(val)
{
_attr = val;
},
get : function()
{
var output = _attr;
console.log("%c> %c"+attr+"%c : "+JSON.stringify(output, undefined, 1), 'color:lightgrey', 'color:red', 'color:black');
return output;
}
});
}(obj, attr))
else
(function(obj, attr){
var _attr = obj[attr];
Object.defineProperty(obj, attr,
{set : function(val)
{
_attr = val;
},
get : function()
{
var output = _attr;
console.log("%c> %c"+attr+"%c : "+output, 'color:lightgrey', 'color:red', 'color:black');
return output;
}
});
}(obj, attr))
}
}
<html>
<script src="radioactive.js"></script>
<script>
var O = function(){
this.log = function(str){ console.log(str); return str+"!";}
this.hello = 'world';
this.objects = {prop:'value'};
this.arrays = [1,2,3]
}
o = new O()
o.log('not activate');
radioactivate( o )
o.log('activate')
o.hello
o.hello = 'earth'
o.hello
o.objects
o.arrays
o.arrays = [4,5,6]
o.arrays
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment