Home  >  Article  >  Web Front-end  >  "JavaScript DOM Programming Art" reading notes - JavaScript syntax_javascript skills

"JavaScript DOM Programming Art" reading notes - JavaScript syntax_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:21:08889browse

Notes
                                                                        Single line comment: //

Multi-line comments: /* */

"" multi-line comment and is easily confused, this comment method is not recommended

Variable
In the JavaScript language, the names of variables and other syntax elements are case-sensitive. A variable named mood has nothing to do with a variable named Mood, MOOD, or mOOd; they are not the same variable.

JavaScript syntax does not allow variable names to contain spaces or punctuation (except for "$").

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

Another way is to use camel case format, remove the white space (underline) in the middle, and start each new word with a capital letter instead: var myMood = "happy";

Data Type

String
The string must be enclosed in quotes, either single or double quotes. You can choose quotes as you like, but it's best to choose them based on the characters the string contains. If the string contains double quotes, put the entire string in single quotes, 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 quotes between the letters "n" and "t" can be regarded as part of the string. In this case we need to escape this character. Use backslashes to escape characters in JavaScript:

var mood = 'don't ask';

Array
Associative array
Traditional array: The subscript of each element is a number, and each time an element is added, the number increases by 1 at a time.

If only the values ​​of the elements are given when filling the array, the array will be a traditional array, and the subscripts of each element will be automatically created and refreshed.

This default behavior can be changed by explicitly giving the subscript for each new element when filling the array. When giving subscripts for new elements, you are not limited to using integer numbers. You can use string:

Copy code The code is as follows:

var lemon = Array();
      lemon["name"] = "John";
Lemon["year"] = 1940;
Lemon["living"] = false;

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

Object

Copy code The code is as follows:

var lemon = Object();
lemon.name = "John";
lemon.year = 1940;
lemon.living = false;

The lemon object can also be written as follows:

Copy code The code is as follows:

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

Comparison operators

The equality operator == does not mean strict equality, which can easily make people confused. For example, what would be the result of comparing false to an empty string?

Copy code The code is as follows:

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 the empty string to have the same meaning as false. To do a strict comparison, use another equal sign (===). This identity operator performs a strict comparison, comparing not only the values, but also the types of the variables.

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn