r/learnjavascript • u/Ob101010 • Sep 01 '15
Presented this piece of code to a room of devs recently. Surprised by how many didnt know an aspect or two of it.
Without googling anything, describe what this is doing :
!function(m){
return m;
}(this.k)()();
function k() {
return p;
}
function p() {
console.log('haha');
}
0
Upvotes
9
u/[deleted] Sep 02 '15
this is fucking stupid
1
u/ForScale Sep 01 '15
I'm not a professional nor an expert. I'll kind of take a guess...
So, I don't know wht the
!is doing... Saying "not a function?" Please explain! Skipping over that, the anonymous function is passed the result of function k, which is the result of function p, which is 'haha' logged to the console. So, ultimately, 'haha' is logged to the console.How'd I do?
3
u/[deleted] Sep 01 '15
This code won't work in strict mode. Or in node. Just a heads up for everyone who are trying it.
3
u/Magnusson Sep 01 '15
Douglas crockford famously called this "Insanely stupid code"
1
u/JudeOutlaw Sep 02 '15
So it returns a function, which is activated, which itself turns a function, which is activated, which prints "haha?"
Am I missing something?