Basic Daily knowledge of JavaScript, basic knowledge of javascript

Source: Internet
Author: User
Tags type null types of functions

Basic Daily knowledge of JavaScript, basic knowledge of javascript

Basic Concepts 

Javascript is an interpreted language, and the browser acts as an interpreter. During js execution, it is explained before execution in the same scope. During the interpretation, the variables defined by function and var are compiled. After compilation, the variables are executed from top to bottom and assigned values.

Case Sensitive 

All variables, function names, and operators in ECMASCript are case sensitive.

1. Variables

The variable is set to memory when it is used for the first time, so that it can be referenced in the script later. Declare variables before using them. You can use the var keyword to declare variables.

Var count, amount, level; // multiple declarations declared with a single var keyword.

Variable name

Variable names include global variables, local variables, class variables, and function parameters.

Variable names are composed of prefix types and meaningful words. The camper naming method is used to increase the readability of variables and functions. For example, sUserName and nCount.
Prefix specification:
Each local variable must have a type prefix, which can be divided:
S: string. Example: sName, sHtml;
N: Number. Example: nPage, nTotal;
B: indicates the logic. Example: bChecked, bHasLogin;
A: array. Example: aList, aGroup;
R: indicates a regular expression. Example: rDomain, rEmail;
F: indicates a function. Example: fGetHtml, fInit;
O: other objects not involved above, such as oButton and oDate;
G: indicates global variables, such as gUserName and gLoginTime;

JScript is a case-sensitive language. Follow the following rules to create valid variable names:

Note that the first character cannot be a number.

It can be followed by any letter, number, or underline, but it cannot be a space variable name or a reserved word.

Javascript is a weak language that ignores unnecessary spaces. You can add spaces to the script to improve readability.

Var is a reserved word in javascript, indicating that the variable name is a user-defined identifier, and variables are separated by commas.

If a variable is declared but not assigned a value, the variable exists and its value is Jscript value undefined.

Forced type conversion 

In Jscript, operations can be performed on different types of values without worrying about exceptions in the JScript interpreter. Instead, the JScript interpreter automatically changes (forced conversion) One of the data types to another, and then executes the operation. For example:

Calculation Result

Adding a value to a string forcibly converts the value to a string.
Adding a Boolean value to a string forcibly converts a Boolean value to a string.
Adding a value to a Boolean value forcibly converts a Boolean value to a value.

To explicitly convert a string to an integer, use the parseInt method. To explicitly convert a string to a number, use the parseFloat method.

JavaScript variable Survival: After you declare a variable in the function, you can only access the variable in the function. After exiting the function, the variable is revoked. This type of variable is called a local variable. You can use local variables with the same name in different functions, because only the declared functions can recognize each of these variables.

If you declare a variable outside the function, all functions on the page can access the variable. The lifetime of these variables starts after they are declared and ends when the page is closed.

Js variable Mind Map

 

2. js Data Type 

Jscript has three types: main data types, composite data types, and special data types.

Main (basic) Data Types
String
Value
Boolean

Composite (reference) Data Type
Object
Array

Special Data Types
Null

'Undefined'

String data type: the string data type is used to represent the text in JScript. In JavaScript, although double quotation marks ("") and single quotation marks (") can represent strings, there is almost no difference between them. However, only double quotation marks ("") are used to indicate that the string is considered to be the best.

A string value is a string of zero or more Unicode characters (letters, numbers, and punctuation marks ).

What is Unicode? 

Unicode provides a unique value for each character, no matter what platform, program, or language. Unicode is developed to provide unified encoding for all characters in the world.

Numeric data type 

We need to understand that JScript internally expresses all the values as floating point values, so there is no difference between integer and floating point values in Jscript.

Boolean data type 

Boolean (logical) can have only two values: true or false.

Js array and Object 

For more information, see my article-> javascript learning Summary--array and object Section

Null data type: You can assign a null value to a variable to clear the content of the variable.

In Jscript, the typeof operator reports the null value as the Object type, rather than the type null.

 

Null indicates an existing object. It is often used to indicate that a function attempts to return a non-existing object.

Undefined data type:

The undefined value is returned as follows:
The object property does not exist,
Declared variables but never assigned values.

Differences between null and undefined

alert(typeof undefined); //output "undefined" alert(typeof null); //output "object" alert(null == undefined); //output "true" 

ECMAScript considers undefined to be derived from null, so they are defined as equal.

alert(null === undefined); //output "false" alert(typeof null == typeof undefined); //output "false" 

The type of null is different from that of undefined, so the output is "false". And = indicates absolute equals. Here null = undefined outputs false

In addition, a more important data type-reference data type is introduced here.

Reference data type

The javascript reference data type is an object stored in the heap memory. JavaScript does not allow direct access to the location and operation in the heap memory space. You can only use the reference address of the operation object in the stack memory. Therefore, data of the reference type is saved in the stack memory as the reference address of the object in the heap memory. You can use this reference address to quickly find objects stored in heap memory.

The following describes how to assign a value to the referenced data type.

 

Naturally, adding the name attribute to obj2 actually adds the name attribute to the object in the heap memory. What obj2 and obj1 save in the stack memory is the reference address of the heap memory object, although it is also copied, it points to the same object. Therefore, changing obj2 causes the change of obj1.

The basic type value refers to the simple data segments stored in the stack memory, that is, the values are completely stored in a location in the memory.
The reference type value refers to the objects stored in the heap memory, that is, the objects stored in the variable are actually a pointer, which points to another location in the memory and stores the objects.

In short, the heap memory stores reference values, and the stack memory stores fixed type values.

 

In ECMAScript, a variable can have two types of values: the original value and the reference value.

Raw values are stored in the simple data segment of the stack, that is, their values are directly stored in the variable access location. The reference value is stored in the heap object. That is to say, the value stored in the variable is a pointer (point) pointing to the memory of the stored object.

<Script type = "text/javascript"> var box = new Object (); // create a reference type var box = "lee"; // the basic type value is the string box. age = 23; // It is strange to add attributes to basic type values because only objects can add attributes. Alert (box. age); // It is not a reference type and cannot be output. </script>

3. JScript Operators 

Priority: The operation sequence of operators. Generally speaking, it is the first part of calculation.
Conciseness: the order in which operators of the same priority are calculated. In general, the value is from which direction, left to right, or right to left.

Data type conversion and basic packaging types

Convert String () to String type
Number () to numeric type
Convert Boolean () to Boolean type

ParseInt: converts a string to an integer. Start from the beginning of the string, stop parsing at the first non-integer location, and return all integers read before. If the string does not start with an integer, NaN is returned. For example, parseInt ("150 hi") returns 150, and parseInt ("hi") returns NaN.
ParseFloat: converts a string to a floating point number. Start from the beginning of the string, stop parsing at the first non-integer location, and return all integers read before. If the string does not start with an integer, NaN is returned. For example, parseFloat ("15.5 hi") returns 15.5, and parseFloat ("hi 15.5") returns NaN.

Eval:Calculate the string as a javascript expression and return the execution result. If there is no result, undefined is returned.
Basic packaging type

Every time a basic type value is read, an object of the basic packaging type is created in the background, so that some methods can be called to operate on the data. The basic packaging types include Boolean, Number, and String.

Var box = 'trigger4'; // literal box. name = 'Mike '; // invalid attribute box. age = function () {// return 22;}; // var box = new String ('trigkit4'); // new operator box. name = 'Mike '; // valid attribute box. age = function () {// valid method return 22 ;};

The String type contains three attributes and a large number of available built-in methods.
Attribute description
Length: returns the length of a string.
Constructor: returns the function used to create a String object.
Prototype: extended string definition by adding attributes and Methods

4. js Process Control 

For js process control statements, it is hard to understand. Do not repeat the rest. And so on.

1.... The in statement corresponds to each object or each element of an array, and executes one or more statements.
For (variable in [object | array])
Statements

Parameters:
Variable: required. A variable, which can be any attribute of an object or any element of an array.
Object, array: Optional. The object or array to traverse.
Statement: Optional. One or more statements to be executed relative to each attribute of an object or each element of an array. It can be a compound statement.

Although conditional control statements (such as if statements) require the use of code blocks only when multiple statements are executed (the end of left curly braces (") {" start, right curly braces ), but the best practice is to always use code blocks.

If (args) alert (args); // if (args) {alert (args); // recommended}

5. js Functions 

A function is an event-driven or reusable code block that is executed when it is called.

Jscript supports two types of functions: internal functions of the language and self-created functions.
JavaScript Functions allow the absence of parameters (but the parentheses containing the parameters cannot be omitted). You can also pass parameters to the functions for the function to use.

Related Article

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.