开发者

Different ways of declaring variables within functions -how do they differ?

开发者 https://www.devze.com 2023-02-02 04:07 出处:网络
I have seen two differen开发者_JS百科t ways to declare variables within functions. How do they differ? Thank you.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号