Different ways of declaring variables within functions -how do they differ?
I have seen two differen开发者_JS百科t ways to declare variables within functions. How do they differ? Thank you.
Namespace.Class = function() {
// first way. use "var".
var variable1 = 'value';
// second way. use "namespace".
Namespace.Class.variable2 = 'value';
};
var
declares a local variable, meaning it's only visible from within the function, while the second way is for declaring a member of the object, which will be visible from everywhere.
A tutorial on Javascript variables: http://www.webdevelopersnotes.com/tutorials/javascript/global_local_variables_scope_javascript.php3
Edit: A tutorial on private members in JS: http://www.crockford.com/javascript/private.html
精彩评论