[Javascript advanced programming] Reading excerpt 1 JavaScript introduction, excerpt javascript

Source: Internet
Author: User

[Javascript advanced programming] Reading excerpt 1 JavaScript introduction, excerpt javascript

I have been developing servers, but I have to develop some front-end pages. Try to get familiar with node. js by the way.

Objective: To perform both the frontend and backend repairs. What.

Chapter 1 Introduction to Javascript

1. Relationship between ECMAScript and javascript

2. DOM level

DOM0 is actually a non-existent standard, which refers to the DHTML initially supported by IE4 and NetScape4

DOM1 mainly maps the structure of the document.

DOM2 expands mouse and user interface events, ranges, traversal, and other modules

DOM3 further expands DOM, introduces methods for Uniformly Loading and saving documents, and adds methods for verifying documents. It supports XML1.0 specifications.

3. Javascript contains three different parts:

(1) ECMAScript

(2) DOM

(3) BOM

Chapter 2: use Javascript in HTML

1. HTML4.01 is an attribute defined by <script>:

(1)Charset(Optional) Character Set of the Code specified by the src attribute.

(2)Defer: Indicates that the script can be executed after the document is completely parsed and displayed (not all browsers support the defer attribute and need to be used with caution)

(3)Language: Obsolete. It is used to indicate the scripting language used to write code. Most browsers ignore this attribute, so it is unnecessary to use it again.

(4)Src: Optional. Indicates the external file of the code to be executed.

(5)Type: Required. It can be considered as an alternative attribute of language, indicating the content type (MIME) of the language script for code writing ). considering the Conventions and the maximum degree of browser compatibility, the currently used attribute value is still "text/javascript"

2. javascript writing compatible with HTML and XHTML:

<Script type = "text/javascript">

// <! [CDATA [

Function compare (a, B ){

If (a <B ){

Return 0;

} Else if (a> B ){

Return 1;

}

Return-1;

}

//]>

3. Advantages of placing javascript in files as much as possible:

(1) Maintainability (put in a separate js file for easier maintenance)

(2) cache (the browser can cache js files according to settings)

(3) Adapt to the Future (the file reference method does not need to use the XHTML or the hack mentioned above)

4. All javascript elements will be parsed in order of their appearance on the page. Only after the previous code is parsed will the code in the subsequent js element be parsed.

Chapter 3 Basic Concepts

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

2. ECMAScript has five simple data types: Undefined, Null, Boolean, Number, and String. It also has a complex data type: Object. ECMAScript does not define different data types for integers and floating-point numbers. The Number type can be used to indicate all values.

3. typeof is used to detect the Data Type of a variable. The returned strings are: "undefined", "string", "boolean", "number", "object", and "function"

4. floating point numbers have floating point traps, so the precision is not as accurate as the integer. Number. MIN_VALUE Number. MAX_VALUE if the result of a calculation exceeds the Javascript range, it is converted to a special Infinity value (negative Number is-Infinity). This value cannot be used in the next calculation. You can use the isFinite () function to determine whether a number is any one.

5. NaN is a special value, which is used to indicate a case where the operation to return a value does not return a value. NaN has two features: 1. any operation involving NaN will return NaN. 2. NaN is not equal to any value, including itself. The IsNaN function is used to check whether a parameter is a NaN.

6. There are three functions that can convert a non-numeric type to a numeric type: Number (), parseInt, parseFloat

7. Object type. Each instance of an Object has the following attributes and methods:

(1) constructor: stores the function used to create the current object.

(2) hasOwnProperty (): used to check whether the current property exists in the current object instance (rather than in the instance prototype ).

(3) isPrototypeOf (object) checks whether an object is a prototype of another object.TODO: a deep understanding of the prototype

(4) toString () returns the string representation of the object

(5) valueOf () indicates the string, value, or Boolean value of the object. It is usually the same as the return value of toString.

(6) The ECMAScript function is not overloaded. If two identical functions are defined, the renamed function only belongs to the latter defined function.

Chapter 4: variables, scopes, and memory problems

1. Basic type and reference type: the basic type value refers to the simple data segment stored in the stack memory, and the reference type value is the object stored in the heap memory, the variable actually stores a pointer pointing to another location in the memory. This location stores objects. You can only add attributes to values of the reference type.

2. A copy is generated when the basic type value is copied. When the referenced type value is copied, the new value and the previous value both point to the same object.

3. All function parameters in ECMAScript are passed by value.

4. the typeof function can be used for basic type detection. For reference type values, it can be used with instanceof detection. According to regulations, all reference types are Object instances. Therefore, true is always returned when detecting whether a reference variable is instanceof Object.

5. execute context and scope:

The execution environment defines other data that variables or functions have the right to access and determines their behavior. Each execution environment has a variable object associated with it. All variables and functions defined in the environment are stored in this object.

The execution environment hasGlobal execution environmentAndFunction execution environment.

The global environment can only access the variables and functions defined in the global environment, but cannot access any data defined in the local environment.

The local execution environment not only has the right to access the variables in the function scope, but also has the right to access its parent environment and even global variables.

Each function creates its own execution environment when it is called. When the execution flow enters a function, the function environment is pushed into the environment stack. After the function is executed, the stack pops up the environment and returns the control to the previous execution environment.

When the code is executed in an environment, a scope chain consisting of variable objects is created. The purpose of the scope chain is to ensure orderly access to all variables and functions that are accessible to the execution environment.

The process of identifier resolution is to verify the first-level identifier search process of the scope chain. The search process always starts from the front end of the scope chain, and then goes back to the back level until the identifier is found.

6. extend the scope chain

(1) catch Block of the try-catch statement

(2) with statement

7. When the var keyword is used to declare a variable, the variable is automatically added to the nearest available environment. If the variable is initialized without declaration, it is automatically added to the global variable (this method is not recommended and is prone to errors and troubles ).

8. Garbage collection mechanism.

Javascript has an automatic garbage collection mechanism. The execution environment is responsible for managing the memory used during code execution. The principle of this garbage collection mechanism is relatively simple: Find out the memory that is no longer in use, and then release the memory occupied by it

(1) mark and clear

The most common method of garbage collection in Javascript is to mark and clear. When a variable enters the environment, it is marked as "entering the environment". When the variable leaves the environment, it is marked as "leaving the environment ". When running, the garbage collector adds a flag to the variables stored in the memory, and removes the variables in the environment and variables referenced by the environment variables, the variable with the flag will be considered as the variable to be deleted.

(2) reference count

Another uncommon garbage collection policy is "reference count", which traces the number of references to a variable to determine whether it is spam. The problem with reference counting is that the garbage of "circular reference" cannot be cleared. For example:

Function problem (){

Varobj1 = new Object ();

Varobj2 = new Object ();

Obj1.aobj = obj2;

Obj2.aobj = obj1;

}

To avoid similar circular references, it is best to manually disconnect them when they are not used.

9. performance issues of the garbage collector. Javascript Memory Management Problems.

Make sure that the minimum memory usage can improve the page performance. The optimal way to optimize memory usage is to save only necessary data for the code in execution. Once the data is no longer used, set its value to null to release its reference (unreference ). Removing a reference does not mean that the memory occupied by this value is automatically reclaimed. The real function of removing a reference is to disallow the value from the execution environment so that the garbage collector can recycle it the next time it runs.


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.