how javascript framework have own workspace? [duplicate]
Possible Duplicate:
javascript create INcode workspace (framework)
In the last few days i have been interesting in how Javascript framework have they own workspace , what i mean is how they can call a variable at any name they want and work with it without involving local variables.
I'm writing now an API and i'm facing the problem of the local variables / function local names , if they already exist.
So , what i have done is looking into how framework works and i saw they use namespace functions like this :
(function(){ .... })()
so i tried to do something alike
<html>
<head>
<script type="text/javascript">
(function() {
function f(toAlert) { alert(toAlert); }
})();
</script>
</head>
<body>
<script>
toAlert("hi");
</script>
</body>
</html>
but it's not working..
so i have few questions :
how to have a workspace in javascript to set any variable name i want (if function too it would be great).
i saw that the frameworks uses "window" command , is it have something to do with the namespace function to "public" the function or w/e?
i'll be glad if you can give me information / tutorials a开发者_StackOverflow社区bout all this thing , how to make it done from beginning to ending
of course it's not working, because your f(toAlert)
function is only visible within the immediately executing function closure scope. And you're also trying to call some function that doesn't exists in the first place. Your closure function is called f
(and has toAlert
parameter), and then you're trying to call function called toAlert
that wasn't defined anywhere. Not in global nor in closure scope.
Scope?
This code may clear things up a bit for you. Read comments.
<script type="text/javascript">
// global scope
function globalFunc(text) {
alert(text);
}
var privateFunc = null;
// function closure scope
(function(){
// closure function
function closureFunc(text) {
// can call global
globalFunc(text);
}
// let's make closure function accessible from global scope
// since privateFunc variable is in global scope
privateFunc = closureFunc;
})();
// call closure function
privateFunc("calling closure function");
// ERROR: this will not work due to function being in closure scope
closureFunc("calling closure function");
</script>
Creating local scope
What does that strange function parentheses actually do?
This is some function:
function name(someParameter) { ... }
Putting it in parentheses and adding some at the end, executes it right away:
(function name(someParameter) { ... })("Parameter text value");
Mind the function parameter...
Why do libraries use local scope?
Libraries usually use local scope to not pollute and more importantly clash with other possible libraries. Think of two libraries that would both define a function called getName
. The last one that would define it would simply override the first one's implementation thus making the first library to malfunction.
When each library creates its own closure scope they can create whatever functions, variables within and use them without the fear of being overridden. Libraries usually just expose some small part into global scope, so other scripts can actually use the library.
(function() {
var closureVar = "I'm local";
globalVar = "I'm global";
// or
window.globalVar2 = "I'm global equivalent";
})();
omitting var
or referencing window
object makes a function or variable global.
The script is not working since function f
is declared inside another function scope (the outer (function(){..})()
) and thus is for local use (inside the same scope) only. That's actually the main point to use this construction, not to clutter global environment with your leftover functions and variables.
There's no toAlert
function--that's a parameter to function f
. f
isn't in scope outside of the immediately-executed anonymous function, however.
If you want to expose f
you need to return it from the anonymous function so you can capture and call it.
精彩评论