Javascript variables, scopes, namespaces

Source: Internet
Author: User

Javascript variables are mainly divided into two types:
I. Global Variables
II. Local variables (variables in the function)

I. global variables: all variables defined in the field of the method body are global variables, and even variables used in the method body are global variables without the var keyword declaration.
Ll. Local variables: variables defined in the method body and declared using the var keyword. (Even if it is the same as the global variable name, it overwrites the global variable .)


Note:
A global variable is used on the current page. iframe in a page does not work. It is only valid on one page.
If JavaScript code is often used, we need to reference the JS script file, but there may be two scripts with the same global variable name, which may cause an error. Because JS does not report errors to these problems, we need to use namespaces to solve these conflicts.
Javascript namespace:
Random. js
[Javascript]
// Obtain the Element object by ID
Function $ (id ){
Return document. getElementById (id );
}
 
// Display the node type name of the object corresponding to the ID
Function myMethod (id ){
Alert ($ (id). nodeName );
}
 
<Span style = "color: # cc0000;"> // This is an external interface.
Window ['mynamespace'] = {}
Window ['mynamespace'] ['$'] = $;
Lt;/span>
The above code has two methods. The red part is the so-called namespace? Because it is not a new mechanism or technology. If you observe carefully, you will find
Window ['mynamespace'] = {}
It is actually a syntax for defining objects. We add an attribute (property name: myNamespace) to the window object, which is an object. Line 2 code
Window ['mynamespace'] ['$'] = $;
Window ['mynamespace'] is an object. We need to add another attribute ['$'] to this object. This attribute is the method $, So we introduce random. after the js file, you want to use random on the page. when using the $ () method in js, use window. myNamespace. $. You can also directly use myNamespace. $ to omit the window.

Of course, there are still many ways to implement namespaces. You can also wrap an object directly (define all methods in the object) so that if the object name is not passed in the outside world. method Name. in this format, you will not be able to access the method in it, reflecting the encapsulation of object-oriented.

This is an excerpt from the love of geming.-^

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.