JavaScript Study Notes

Source: Internet
Author: User
Tags access properties

1. function test ()
{
Var message = "hi"; // local variable
}
Test ();
Alert (message); // error! Function test ()
{
Message = "hi"; // The global variable omitting the var operator and creates a global variable.
}
Test ();
Alert (message); // "hi"
However, it is not recommended to write the above global variables, which is not easy to maintain.
 

2. The uninitialized and undeclared variables run the typeof operator and return the "undefined" value. Undefined values are automatically assigned to uninitialized variables, but Explicit initialization is recommended because when the typeof operator returns
When the value is "undefined", it is clear that the variable is not declared.
 

3. The switch statement uses the full operator to compare values, so no type conversion occurs (for example, the string "10" is not equal to the value 10)
 

4. Function Parameters
 

5,
 

6. There are two ways to create an Object instance. The first is to use the new operator, as shown below:
Var person = new Object ();
Person. name = "Shrek"
Person. age = 100; the second method is called the literal representation of an object, which is used to simplify the process of creating an object containing a large number of attributes. The attribute and value are separated by a colon, and the attribute are separated by a comma. The last attribute is not separated by a comma.
 
Var person = {
Name: "Shrek ",
Age: 100
};


7. In addition to using dot notation to access object attributes, JavaScript can also use square brackets notation to access object attributes. Attribute to be accessed when square brackets are used
Enclose the string in square brackets. For example, alert (person ["name"]); equivalent to alert (person. name );
There is no difference between the two functions. However, if the attribute name contains characters that may cause syntax errors, or the attribute name uses keywords or reserved words, square brackets can be used.
For example, person ["first name"] = "Shrek ";
Unless you must use variables to access properties, we recommend that you use dot notation.
 

8. The length of the array is not read-only. You can set this attribute to remove items from the end of the array or add new items to the array. Undefined is used to reduce the end of the blocked length or to increase the length.
The length attribute can also be used to easily add new items at the end of the array, as shown below:
Var colors = {"red", "blue", "green "};
Colors [colors. length] = "black ";
Colors [colors. length] = "brown"; an array can contain up to 294, 967, and 295 Items.
 

 

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.