Wednesday, March 26, 2014

Functions knowing what called them:

//this accesses the object
//callee.caller only gets the function that called it.
function dookie(pooper){
console.log('plop');
pooper.speak();
}
function dookie2()
{
console.log("splurt");
try{
console.log(arguments.callee.caller.name);
// doesn't access the object.
}
catch(e){
console.log("doesn't work "+ e);
}
}
var petrov = {
speak : function hiBuddy(){
console.log("yap!");
},
poop : function(){
dookie(this);
},
poop2 : function pooping(){
dookie2();
}
};
petrov.speak();
petrov.poop();
petrov.poop2();
view raw gistfile1.js hosted with ❤ by GitHub

No comments:

Post a Comment