Java programmers from stupid birds to cainiao (28) JavaScript summary language basics

Source: Internet
Author: User
Tags variable scope

As a powerful and widely used programming language, JavaScript scripting language is based on data types, variables, operators, functions, and core statements. This article mainly introduces the basic knowledge of JavaScript scripting language.

I. Basic Knowledge

1. script Execution sequence: the Javascript script interpreter interprets program statements according to the sequence in which program code appears. Therefore, you can place the function definition and variable Declaration between

2. Case sensitivity: JavaScript scripts are case-sensitive, with the same letters and different cases representing different meanings.

White space characters

3. Blank ______ white characters include spaces, tabs, and line breaks. It occupies a certain amount of space when writing script code, but the script is interpreted and executed by the browser without any function. Script programmers often use spaces as blank characters. The Javascript script interpreter ignores any unnecessary spaces.

Note: In a string, spaces are not ignored, but are displayed as part of the string. When writing Javascript script code, you often need to add appropriate spaces to clarify the script code hierarchy, it is convenient for relevant personnel to view and maintain.

4. semicolon: When writing a script statement, use the semicolon as the end character of the current statement.

5. Block: when defining a function, use braces "{}" to encapsulate the function body.

Ii. Data Types

The first thing to note is that there is no specific data type modifier in Javascript. The basic data class, no matter what type, uses var for definition.

1. Integer and floating point value: Javascript allows two types of values: integer and floating-point. The integer type includes a positive integer, 0, and negative integer. the floating-point number can be a real number containing the decimal point, it can also be a real number expressed in scientific notation.

Octal and hexadecimal: Numbers of the integer type can be decimal, octal, or hexadecimal.

Iii. Variables

1. Variable identifier: Unlike C ++, Java, and other advanced programming languages that use multiple variable identifiers, JavaScript scripting uses the keyword VaR as its unique variable identifier, it is used to add the variable name after the keyword var.

2. variable scope: To discuss the scope of variables, we must first understand the relationships and differences between global variables and local variables:

��Global variables: can be called anywhere in the script. The scope of global variables is the entire script area in the current document.

�� Local variables: these variables can only be used within the function to which the declaration statement belongs. The scope of local variables is only the function body.

When declaring a variable, you must decide whether to declare the variable as a global variable or a local variable based on the purpose of programming. Generally, variables that save global information (such as the original table size and string array corresponding to the drop-down box) must be declared as global variables, the variables that save temporary information (such as the format string to be output and intermediate variable for mathematical operations) are declared

Is a local variable.

3. Weak type: Similar to other programming languages, JavaScript scripts have data types for their variables. The specific data types are described in the next section. Advanced programming languages, such as C ++ and Java, are strongly typed languages. Different from this, JavaScript scripting is a weak type language and does not need to explicitly specify its data type when declaring variables, the data type of a variable is derived based on the specific content of the variable and automatically changed based on the change in the content of the variable. When declaring a variable, a strong language must explicitly specify its data type. When declaring a variable, you do not need to explicitly specify its data type, which is both an advantage and a disadvantage of the Javascript script language. The advantage is that you do not need to specify the data type when writing the script code, so that the variable declaration process is simple and clear; the disadvantage is that it may cause fatal mistakes due to misspelling.

4. Basic data types:In program code that implements the predefined function, variables are generally defined to store data (as initial values, median values, final values, or function parameters ). Variables include multiple types. The basic data types supported by the Javascript script language include:

Number type, string type, Boolean Type, undefined type, null type, and function type. The following describes several data types:

4.1 Number Type:Number-type data is numeric data, including integer and floating-point data. The integer-type data can be identified in decimal, octal, and hexadecimal notation, while the floating-point data is a real number containing the decimal point, it can be expressed by scientific notation. Generally, the number type data is a number not in parentheses.

4.2 undefined type:The undefined type is an undefined type. It is used for attributes of variables or objects that do not exist or are not assigned an initial value. For example, the following statement defines the variable name as the undefined type:

VaR name; after defining undefined variables, you can assign values to them in subsequent script code to automatically obtain the data types determined by their values.

4.2 null type:NULL data indicates a null value, which indicates the value of a Data vacancy. It is generally used to set an existing variable (or object attribute) to null. It is difficult to distinguish undefined and NULL data. Generally, the undefined and NULL data types are treated similarly.

4.3 function type:The function type represents a function. You can use the new operator and constructor function () to dynamically create the required function and add the function body to it. For example:

VaR myfuntion = new function ()

{Staments ;};

In addition to the preceding six basic data types, the Javascript scripting language also supports combination types, such as array and object. The following describes the combination types.

Iv. Combination Type

1. array type:Array is an array, which is a sequence containing basic and composite data. In JavaScript scripting, each data type corresponds to an object, and an array is essentially an array object. The following definition is provided:

VaR score = [56, 34, 45];

The preceding statement creates an array score. The members in the brackets "[]" are array elements. Javascript is a weak type language, so the data types of each element in the target array are not required to be the same, for example, VAR score = [, 34, "23", 76, "45"];

Because the array is essentially an array object, you can use the new operator to create a new array, for example, VAR score = new array (56,34, "23", 76, "45 ");

To access a specific element in an array, you can use the index of the element. For example, if the following statement declares the variable M, the fourth element in the array score is returned:

VaR M = score [3];

Array as an array object, with the most important attribute length, used to save the length of the array

2Object Type: An object can be a combination type that contains basic and composite data. Its members act as the attributes of the object, and its member functions act as the object's methods. In JavaScript scripting, you can add a period "." After an object and an object

The name of an attribute (or method) to access the attributes (or methods) of an object. For example:

Document. bgcolor

Document. Write ("Welcome to JavaScript world !");

5. Operators

1 Value assignment operator: The assignment operators in Javascript scripting include "=", "+ =", "-=", "* =", "/=", "% =", and "& =", "^ =", etc.

Brief descriptions of Operators

= M = N: Assign the value of the variable on the right of the operator to the variable on the left.

+ = M + = n Add the values of the variables on both sides of the operator and assign the result to the variable on the left.

-= M-= N: subtract the values of variables on both sides of the operator and assign the result to the left variable.

* = M * = N: multiply the values of variables on both sides of the operator and assign the result to the left variable.

/= M/= N: divide the values of variables on both sides of the operator and assign the result of division to the left variable.

% = M % = n divide the values of variables on both sides of the operator and assign the remainder to the left variable

& = M & = n perform bitwise AND operation on the values of variables on both sides of the operator and assign the result to the left variable.

^ = M ^ = n perform bitwise OR operations on the values of variables on both sides of the operator and assign the result to the left variable

<= M <= N: shifts the value of the variable on the left of the operator to the number of digits specified by the value of the variable on the right, and assigns the operation result to the variable on the left.

>>= M >>> = n shifts the value of the variable on the left of the operator to the right by the number of digits specified by the value of the variable on the right, and assigns the operation result to the variable on the left.

>>>= M >>>> = n logically shifts the value logic of the variable on the left of the operator to the right by the number of digits specified by the value of the variable on the right, and assigns the operation result to the variable on the left.

The value assignment operator is the most common operation when writing Javascript script code. Readers should be familiar with the functions of each operator to avoid obfuscation of its specific functions.

2 basic mathematical operators:In JavaScript scripting, basic mathematical operations include addition, subtraction, multiplication, division, and remainder.

Operators include "+", "-", "*", "/", and "%"

3. bitwise operators:The basic bitwise operators supported by JavaScript scripting include "&", "|", "^", and "~". . When the script code executes the bitwise operation, it first converts the operand to the binary number. After the operation is completed, the return value is converted to decimal.

Bitwise operators

& 9 & 4 the bitwise AND. If the two data bits are both 1, the bitwise is 1; otherwise, the bitwise is 0.

^ 9 ^ 4 returns an exclusive bitwise OR. If the two data correspond to the opposite bitwise, the bitwise is 1; otherwise, the bitwise is 0.

| 9 | 4 by bit or, if the two data bits are both 0, then this bit is 0, otherwise it is 1

~ ~ 4 non-bitwise. If the corresponding data bit is 0, this bit is 1; otherwise, it is 0.

Bitwise operators are widely used in data processing and logic judgment. The bitwise operators can be used properly to save a lot of script code.

3. Auto-increment and auto-subtraction:The auto-increment operator is "++" and the auto-increment operator is "--", respectively. The operand is added by 1 or minus 1. It is worth noting that,

The auto-increment and auto-subtraction operators are placed before and after the operands. If the operator is written before the variable name, the return value is

Is the value before auto-increment or auto-increment. If it is written after, the return value is the value after auto-increment or auto-increment.

4. Comparison Operators: In JavaScript scripting language, the operators used to compare two data are called comparison operators, including "=" and "! = ","> "," <"," <= ","> = ", Etc.

5. logical operators:The logical operators in Javascript scripting include "&", "|", and "!". Is used for operations between two logical data types. The returned data type is boolean.

6. Comma OPERATOR:When writing Javascript script code, you can use the comma "," To connect multiple statements. When the browser loads the code, it is called as a complete statement, however, the return value of a statement is the rightmost statement. Comma (,) is generally used to separate multiple parameters when a function is defined and called,

7. Object Operators: The javascript scripting language mainly supports four object operators, including vertex operators, new operators, delete operators, and () operators. The object contains attributes and methods. The dot operator is used to access the attributes and methods of the object. It is used to separate object names from object attributes (or methods) with dots, for example:

VaR mycolor = Document. bgcolor;

Window. Alert (MSG );

Of course, you can also use the double quotation mark "[]" to access the attributes of the object. Rewrite the preceding

Statement: var mycolor = Document ["bgcolor"];

The new operator is used to create a new object. For example, to create a new array object, you can write it as VAR exam = new array );

The new operator can create a programmer-Defined Object to create an instance of the Javascript built-in object.

8. typeof OPERATOR:The typeof operator is used to indicate the Data Type of the operand. the return value type is a string. In JavaScript scripting, the format is as follows:

VaR mystring = typeof (data );

6. Javascript loop control structure: as JavaScript contains if, if ...... Else, while, do ...... While and other statements are basically familiar with Java, so I will not repeat them here

7. Functions

The javascript scripting language allows developers to combine reusable script code blocks by writing functions, increasing the structuring and modularization of script code. A function transmits data through the parameter interface to implement specific functions.

1. Basic composition of functions

A function is composed of two parts: Function Definition and function call. You must first define the function and then call it to develop a good programming habit. Keywords should be used for function definition.

The syntax rules are as follows:

Function funcname ([parameters])

{Statements;

[Return expression;]}

In addition to user-defined functions, the Javascript scripting language provides a large number of built-in functions that can be called directly without being defined by developers. For example, the alert () method of the window object is a built-in function supported by the Javascript scripting language. After the function definition process ends, you can call the function anywhere in the document. When referencing a target function, you only need to add parentheses after the function name. If the target function needs to introduce parameters, you must add them in parentheses. If a function returns a value, you can assign the final result to a custom variable and return it with the keyword return.

2. Global functions and local functions:The javascript scripting language provides many global (built-in) functions that can be called directly during Script Programming. Here we introduce four simple global functions: parseint (), parsefloat (), and escape () and Unescape ().

The parseint () function is used to convert a string to an integer. The parsefloat () function is used to convert a string to a floating point number () the function is used to convert some special characters into ASCII codes, while the function of Unescape () is used to convert ASCII codes into characters.

3. As an object function:All data types and arrays in Javascript scripting can be treated as objects, and functions are no exception. You can use the new operator and the function object's constructor function () to generate the function of the specified rule.

Method:

VaR funcname = new function (arguments, statements ;);

It is worth noting that the first letter of the above constructor function () must be in uppercase, and the parameter list of the function is separated by a comma.

Note: when defining a function object, the parameter list can be empty or contain one or more parameters. When using a variable to reference the function, parameters required for function execution should be passed to the function body. The most important property of an object's function is that it can create static variables and add instance attributes to the function, so that the function can also play a role between calls. After using static variables as object functions, they can be used to save the running environment parameters, such as the median value.

4. Precautions for function applications

Finally, we will introduce several issues that should be paid special attention to when using functions to help readers use functions better and more accurately and develop good programming habits. The details are as follows:

Define the Function Location: If the function code is complex, there are many calls between functions, the definition of all functions should be placed between the

�� Function naming: The naming principle of a function is the same as that of a variable, but do not name a function or a variable as much as possible. If you need to define a function with a similar name as a variable, you should also add clearly identifiable characters (such as the prefix func) to the function;

�� Function return value: When the function definition code ends, return using the return statement, even if the function does not need to return any value;

�� Scope of variables: identifies whether the variables used in a function are global variables or local variables to avoid errors that are difficult to check during the call process;

�� Function comment: When writing script code, you should add comment statements to specific lines of the Code where appropriate, for example, you can comment out the number of parameters, data types, return values, and functions of a function, which facilitates developers to maintain the program in the future, and facilitates others to read and use the function, facilitating Modular programming;

�� Function parameter passing: Because Javascript is a weak type language, when a variable is used, it does not check its data type, resulting in a potential threat, that is, when a developer calls a function, the number of parameters passed to the function or the data type does not meet the requirements, leading to errors. When calling a function, you should carefully check the number and Data Type of parameter variables passed to the target function.

The fifth point is particularly worth special attention, and the errors caused by it are very difficult to be detected.

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.