A. JavaScript syntax directory
- Grammar
- Operation
- Conditional statements
- Looping statements
- Function
- Object
Two. Specific content
2.1 Syntax
JavaScript code can be executed through the html/xhtml document. There are two ways to do this, the first of which is to place the JavaScript code between the <Script> tags in the document
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Test</title> <Script>JavaScript goes here! </Script> </Head><Body>Mark up goes here!</Body></HTML>
Another better way is to store JavaScript code as a standalone file with a. js extension, typically by placing a <script> tag in the
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Test2</title> <Scriptsrc= "Mairr.js"></Script></Head><Body>Mark up goes here! <!--among them,<script> tags are best placed before the </body> tag, allowing the browser to load the page more quickly, and later chapters will detail -</Body></HTML>
(1) statement
A series of instructions that are called statements. JS's statement separation requirements are not so strict, line breaks can represent the end of the statement, of course, for programming habits, it is best to add a semicolon. (Three kinds of programming are correct, it is recommended to choose the last one)
<!-- Direct carriage return and line break - First Statementsecond statement <!-- semicolon separated - First statement; second statement; <!-- semicolon and Wrap - First statement; second statement;
(2) Notes
- Single-line comment://...
- Block Comment:/* .... */
- Comments in HTML documents: <!--This is the part that you want to comment out--
- One half of JavaScript can be commented on as a line: <!--This is just a drop of the line content, the latter part of the parentheses may not be required
(3) Variables
JavaScript allows you to assign values directly to a variable without having to declare it in advance (weak programming language). If the value is not declared before a variable is assigned, the assignment automatically declares the variable. For programming habits, it is best to declare variables, for example: Var name,age.
The name of the variable should meet the following principles:
- The names of variables and other syntactic elements are case-sensitive;
- Spaces and punctuation are not allowed in variable names (except $ for dollar sign);
- Variable names are allowed to contain letters, numbers, dollar signs (&), but the first character is not allowed to be numbers;
- Longer variable names are suggested with underscores to increase readability; (var my_name;);
(4) Data type
A. String
Strings include (but are not limited to) letters, numbers, punctuation marks, and spaces. Strings must be enclosed in quotation marks, either single or double quotes. Sometimes there are single or double quotes inside the string, and the escape character (\) is required, and in JavaScript, the character is escaped with a backslash: var mood = ' don\ ' t ask '; (to avoid and but not to be confused with quotation marks).
B. Boolean values
Boolean data has only two optional values---->ture/false. (Note that Ture/false is not enclosed in quotation marks, and will be treated as a string assignment)
C. Numerical value
JavaScript allows the use of numeric values with decimal points, and allows any number of decimal places, which are called floating-point numbers, and a minus sign (-) is added to indicate negative values.
(5) array
JavaScript simply does not need to give the number of elements when declaring an array, and adding an element to an array is called a fill. var person = Array ("Mairr", 1949,ture).
(6) Object
Like arrays, objects use a name to represent a set of values. Each value of the object is a property of the object. For example:
var Mairr == "HuGe"=false;
2.3 Operation
Arithmetic operators (including add, subtract, multiply, divide, parentheses, self-increment + +, self-subtract-). The operation of connecting multiple strings to the end is called stitching. As shown below, the stitching example:
// Direct Stitching var message = "I am Feeling" + "sad"; // through variable completion • Stitching var mood = "sad"; var message = "I am feeling" + mood;
2.4 Article statements
If conditional statement BASIC syntax structure: if (condition) {statements;}
(1) Comparison operators
- Basic Comparator ">,<,<=,>=,"
- No, no! " =
- Note the difference between several equals:
- "=" denotes an assignment symbol;
- "= =" does not mean strict equality, for example, using the "= =" number to compare false with an empty string, will return true, not strictly equal;
- "= = =" means strict equality;
(2) Logical operators
- Logic and "&&"
- Logical OR "| |"
- Logical NOT "!"
(The result of the logical operation is the Boolean true/false)
2.5 Loop Statements
(1) While loop
// Way One while (condition) { statements;} // Mode two Do { statements;} while (condition);
(2) For loop
for (initial condition; test condition; alter condition) { statements;}
2.6 Functions
If you need to use the same piece of code multiple times, you can encapsulate them as a function. A function is a set of statements that are allowed to be called at any time in your code. Each function is actually a short script.
The syntax for defining a function is as follows:
function name (arguments) { statements;}
A variable can be either global or local. This is the scope of the variable.
Global variables can be referenced anywhere in the script. Once you declare a global variable in a script, you can refer to it from anywhere in the script, including inside the function. The scope of the global variable is the entire script.
A local variable exists only inside the function that declares it, and cannot be applied outside of that function, and the scope of the local variable is limited to a specific function.
If Var is used in a function, that variable is treated as a local variable that exists only in the context of the function, whereas if VAR is not used, that variable is treated as a global variable, and if the script already has a global variable with the same name, the function will change the global variable's straight.
Examples are as follows:
function Square (num) { = num *num; // here is the global variable, when set to var total = num *num;---> is the local variable return Total ;} var total =; // Here is a local variable var number = square,alert (total);
The resulting results are:
The value of the global variable total is changed to 400 instead of the local variable of 50.
2.7 Objects
An object is a data type that can be accessed in two forms by the data contained in the object-the property and method:
- A property is a variable that is subordinate to a particular object;
- Method is a function that only a particular object can call;
An object is a data entity that is composed of properties and methods, and in JavaScript, properties and methods are accessed using the "dot" syntax:
- Object.property
- Object.Method ()
Creating a new instance of a given object requires the use of the New keyword
var new person;
2.7.1 Built-in objects
JavaScript provides a set of pre-defined objects that can be used to refer to the built-in objects. There are many built-in objects in JavaScript, including the array object, the Math object, and the Date object. For example, you can create a new instance directly and then call its properties/methods:
// Example One var New Array (); beatles.length; // Example Two var New Date (); var taday = Current_date.getday ();
2.7.2 Host Object
In addition to the built-in objects, JavaScript scripts use some other objects that have already been predefined. These objects are not provided by the JavaScript language province, but are provided by his operating environment. specifically to Web applications, this environment is the browser. The predefined objects provided by the browser are called host objects.
Host objects include form, Image, element, and so on. We can use these objects to get information about forms, images, and various table elements on a Web page.
JavaScript DOM Programming Art (1)---> JavaScript syntax