JavaScript DOM Programming Art (2nd edition) reading notes (2)

Source: Internet
Author: User
Tags integer numbers

JavaScript Syntax comments

Single-line Comment://

Multiline Comment:/* */

"<!--" can be used as a single-line comment, which is confusing because it is similar to the HTML "<!---->" Multiline comment, so this annotation method is not recommended

Variable

In the JavaScript language, the names of variables and other grammatical elements are case-sensitive. The variable with the name mood has no relation to the variable whose name is mood, mood, or mood, and they are not the same variable.

The JavaScript syntax does not allow a variable name to contain spaces or punctuation marks ("$" exceptions).

JavaScript variable names allow you to include letters, numbers, dollar signs, and underscores (but the first character is not allowed to be a number).

Another way is to use the hump format, delete the middle blank (underscore), followed by each new word instead of starting with capital letters: var Mymood = "Happy";

Data type String

The string must be enclosed in quotation marks, either single or double quotation marks. You can choose the quotation marks at will, but it is best to select them based on the characters contained in the string. If the string contains double quotes, enclose the entire string in single quotation marks, and vice versa:

var mood = "Don ' t ask";

If you want to use single quotes in the above statement, you must ensure that the single quotation marks between the letters "n" and "T" can be used as part of the string. In this case we need to escape this character. To escape a character in JavaScript with a backslash:

var mood = ' don\ ' t ask ';

Array associative array

Traditional arrays: The subscript for each element is a number that increments by 1 for each additional element.

If the value of the element is given only when the array is populated, the array will be a traditional array, and the subscripts of its individual elements will be automatically created and refreshed.

This default behavior can be changed by explicitly giving a subscript to each new element when populating the array. When you give a subscript to a new element, you do not have to limit the use of integer numbers. You can use a string:

var lemon = Array ();

lemon["name"] = "John";

lemon["Year"] = 1940;

lemon["Living"] = false;

Such an array is called an associative array. Because strings can be used in place of numeric values, the code is more readable. However, this usage is not a good habit and is not recommended for everyone to use. Essentially, when you create an associative array, you create an array object's properties. In JavaScript, all variables are actually objects of some kind. For example, a Boolean value is an object of type Boolean. In the above example, you are actually adding the name, year, and living three properties to the lemon array. Ideally, you should not modify the properties of an array object, but you should use a generic object.

Object

var lemon = Object ();

Lemon.name = "John";

lemon.year = 1940;

Lemon.living = false;

Lemon objects can also be written as follows:

var lemon = {Name: "John", year:1940, living:false};

Comparison operators

The equality operator = = does not mean strict equality, which is easy to confuse. For example, what would be the result of comparing false with an empty string?

var a = false;

var B = "";

if (a = = b) {

Alert ("a equals B");

}

The evaluation result of this conditional statement is true, why? Because the equality operator = = considers an empty string to have the same meaning as false. To make a strict comparison, use a different equals sign (= = =). This equality operator performs a strict comparison, not only comparing values, but also comparing the types of variables.

Of course, this is true for the unequal operator! =. If you want to compare strictly unequal, you should use!==.

JavaScript DOM Programming Art (2nd edition) reading notes (2)

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.