The basic knowledge _javascript skills that JavaScript must learn every day

Source: Internet
Author: User
Tags arrays numeric value reserved type null wrapper

Basic Concepts

JavaScript is an interpreted language, and the browser acts as an interpreter. When JS executes, it is explained before execution in the same scope. The definitions of function and Var are compiled and then executed from the top down and assigned to the variable when the compilation is complete.

Case Sensitive

Everything in the ECMAScript (including variables, function names, and operators) is case-sensitive.

1. Variable

Variables are set in memory the first time they are used, so that they can be referenced later in the script. Declare the variables before using them. You can use the VAR keyword to declare a variable.

var count, amount, level; Multiple declarations declared with a single var keyword.

Variable naming

Variable names include global variables, local variables, class variables, function parameters, and so on, and they all fall into this category.

Variable names are made up of type prefixes + meaningful words, and the hump-style nomenclature increases the readability of variables and functions. For example: Susername,ncount.
Prefix Specification:
Each local variable needs to have a type prefix, which can be divided by type:
S: Represents a String. For example: sname,shtml;
N: Represents a number. For example: npage,ntotal;
B: Presentation logic. For example: Bchecked,bhaslogin;
A: Represents an array. For example: Alist,agroup;
R: Represents a regular expression. For example: Rdomain,remail;
F: Represents a function. For example: Fgethtml,finit;
O: Represents other objects not covered above, such as: obutton,odate;
G: Represents a global variable, such as: Gusername,glogintime;

JScript is a case-sensitive language. Creating a valid variable name should follow the following rules:

Note that the first character cannot be a number.

Can be followed by any letter or number and underline, but cannot be a space variable name must not be a reserved word.

JavaScript is a weakly typed language, and JavaScript ignores extra spaces. You can add spaces to the script to improve its readability.

var is a reserved word for JavaScript, indicating that the next is the variable description, the variable name is a user-defined identifier, and the variables are separated by commas.

If a variable is declared but has not been assigned a value, the variable exists with the value of the JScript value undefined.

Force type Conversions

In JScript, you can perform operations on different types of values without worrying about the JSCRIPT interpreter producing an exception. Instead, the JScript interpreter automatically changes one of the data types (casts) to another data type, and then performs the operation. For example:

Operation result

Numeric values are coerced into strings by adding values to the string.
The Boolean value is added to the string to cast the Boolean value to a string.
The value is added to the Boolean value to cast the Boolean value to the numeric 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.

The lifetime of a JavaScript variable: When you declare a variable within a function, you can access the variable only in that function. When the function is exited, the variable is revoked. This variable is called a local variable. You can use local variables with the same name in different functions, because only the function that declares the variable can recognize each variable.

If you declare a variable outside of a function, all functions on the page can access the variable. The lifetime of these variables starts after declaring them, and ends when the page closes.

JS Variable thinking Guide map

2.js Data Type

JScript has three types of-> primary data types, two-> composite data types, and two-> special data types.

Primary (Basic) data types
String
Numerical
Boolean

Composite (reference) data type
Object
Array

Special data types
Null

' Undefined '

String data type: The string data type is used to represent text in JScript. In JS, although double quotes ("") and single quotes (") can represent strings, they are almost indistinguishable. However, only double quotes ("") are used to indicate that the string is considered best.

A string value is a series of 0 or 0 Unicode characters (letters, digits, and punctuation marks) that are queued together.

What is Unicode?

Unicode provides a unique numeric value for each character, regardless of platform, program, or language. Unicode is developed to provide a unified code for handling all the characters that exist in the world.

Numeric data Types

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

Boolean data type

Boolean (logic) can have only two values: TRUE or FALSE.

JS Arrays and Objects

See my article for details->javascript learning summary--arrays and object parts

Null data type: You can clear the contents of a variable by assigning a null value to a variable.

The typeof operator in JScript reports that the null value is type Object, not type null.

  
 

Null is used to represent an object that does not exist, and is commonly used to represent a function attempt to return an object that does not exist.

Undefined Data type:

The undefined value is returned as follows:
object property does not exist,
A variable was declared but never assigned a value.

The difference between null and undefined

Alert (typeof undefined); Output "undefined" 
alert (typeof null);//output "Object" 
alert (null = = undefined);//output "true" 

ECMAScript that undefined are derived from null, so they are defined as equal.

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

Null is not the same type as the undefined, so the output is "false". and = = = = Absolute equals, here null = = undefined output false

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

Reference data type

JavaScript reference data types are objects that are stored in heap memory, and JavaScript does not allow direct access to the location in the heap memory space and the operating heap memory space, only by manipulating the reference address of the object in stack memory. So the reference type of data, stored in the stack memory is actually the object in the heap memory reference address. This reference address allows you to quickly find objects that are saved in heap memory.

Let's demonstrate this reference data type assignment procedure

Naturally, adding the name attribute to the OBJ2 is actually adding the name attribute to the object in the heap memory, and OBJ2 and obj1 are storing only the reference addresses of the heap memory objects in the stack memory, although they are copied, but the same object is pointed to. Therefore, the change of OBJ2 caused obj1 change.

The base type value refers to the simple data segments that are stored in the stack memory, that is, a value that is stored completely in one place in memory.
Reference type values refer to objects that are stored in heap memory, which is actually just a pointer to another location in memory, where the object is saved.

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

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

Raw values are stored in simple data segments in stacks (stack), that is, their values are stored directly in the location where the variable is accessed. The object that the reference value is stored in the heap (heap), that is, the value stored at the variable is a pointer (point) that points to the memory where the object is stored.

 <script type= "Text/javascript" >
var box = new Object ();//Create a reference type
var box = "Lee";  The base type value is the string
box.age =;  Basic type value adding attributes are weird because only objects can add properties.
alert (box.age);//is not a reference type and cannot be exported;
</script> 

operator of the 3.JScript

Precedence: Refers to the operation order of operators, in layman's terms, which part is calculated first.
Binding: The calculation Order of the same priority operator, in layman's terms, is left to right or right to left.

Data type conversions and basic wrapper types

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

Parseint: Converts a string to an integer. Begins parsing at the beginning of a string, stops parsing at the first non-integer position, and returns all the integers previously read. If the string does not start with an integer, it returns Nan. For example: parseint ("HI") returns the value: 150,parseint ("HI") returns the value: NaN.
Parsefloat: Converts a string to a floating-point number. Begins parsing at the beginning of a string, stops parsing at the first non-integer position, and returns all the integers previously read. If the string does not start with an integer, it returns Nan. For example: parsefloat ("15.5 hi") returns the value: 15.5,parsefloat ("Hi 15.5") returns the value: NaN.

eval: evaluates the string as a JavaScript expression, returns the execution result, and returns undefined if there are no results.
Basic Packing Type

Each time a base type value is read, the background creates an object of the corresponding base wrapper type, so that some methods can be invoked to manipulate the data. Basic wrapper types include Boolean, number, and string

 var box = ' TRIGKIT4 '; Literal amount
Box.name = ' Mike ';  Invalid attribute
box.age = function () {//invalid method return
  ;
};

The new operator is written in
var box = new String (' trigkit4 ');//new operator
box.name = ' Mike ';  Valid attribute
box.age = function () {//effective method return
  ;
}; 

The string type contains three properties and a large number of available built-in methods
Attribute description
Length: Returns the character lengths of the string
Constructor: Returns the function that created the string object
Prototype: extending string definitions by adding properties and methods

4.js Process Control

For JS Process Control statements, here only a few more difficult to understand. Others do not repeat. Please attach a mind map.

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

Parameters:
Variable: Required option. A variable that can be either a property of object or any element of an array.
Object, array: Optional. The object or array to traverse on.
Statement: Options available. One or more statements to be executed relative to each attribute of object or to each element of the array. Can be a compound statement.

Although a conditional control statement, such as an if statement, requires the use of a code block (the left curly brace "{" begins with the closing curly brace "}") only if multiple statements are executed, the best practice is to always use code blocks.

 if (args)
  alert (args),//error prone

if (args) {
  alert (args);//Recommended
}

5.js function

A function is an event-driven or reusable block of code that executes when it is invoked.

Jscript supports two functions: one is a function within the language and the other is created by itself.
JavaScript functions allow no arguments (but parentheses that contain parameters cannot be omitted) or pass arguments to functions for use.

Learn more about functions please visit my other article: JavaScript learning Summary (iv) function part

The composition of the object
Method-Function: process, dynamic
Properties--variables: state, static

Finally, attach a picture of the previous summary of the mind map:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.