Not
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
I have been staring at javascript in Windows 8, and it just seems weird. That anonymous thing with the empty set of parentheses at the end of the file. Why is it anonymous, is it a criminal on the look out for the police?
Here is what the code in one of the templates called “Blank App”, I don’t show the code above it.
(function () {
//Code removed for brevity sake
};
app.start();
})();
Why the empty parentheses? This makes the function “anonymous”, why is the function “anonymous”? Because it has no name! It’s true. This also means that we are able to do some cool things with the function. For instance we are able to control controls. Really, I didn’t just repeat the word control twice, one was the verb control and the other was the noun for objects that the user or system can interact with. Sometimes we need to know the exact order of when controls are used.
This can be done using anonymous functions. More in later blogs.
Comments
Anonymous
August 22, 2012
You do this so that your code inside the outer function doesn't pollute the global namespace when you declare functions and variables. This is not to make it "anonymous".Anonymous
August 22, 2012
Emmm.. your sample code contains unbalanced curly brackets. Btw, when you give the function a name and take it out, then replace the original part with the function name, it's really not that weird.Anonymous
August 23, 2012
cheong00, Oops, unbalanced are my curly bracket and so is my brain. Thank you for the feedback. As to being weird, again, you are right. But when I think about it, all of software is weird. So JavaScript anonymous functions are part of the set of weird. See set theory in use!Anonymous
August 23, 2012
Jesse, Thank you for the comment, and you are right.