"JavaScript advanced Programming" reading notes

Source: Internet
Author: User
Tags arithmetic operators bitwise operators hosting web hosting

Chapter Javascipt Introduction

The complete javacript consists of the following three parts: Core (ECMAScript), Document Object Model (DOM), Browser object model (BOM)

core (ECMAScript): Defined by ECMA-262, providing core language functionality

ECMA-262 definition:

(1) syntax (2) Type (3) statement (4) keyword (5) reserved word (6) operator

A Web browser is just a ECMAScript hosting environment that provides not only the basic ECMAScript implementation, but also the expansion of the language for language and environment interaction.

For example: Extending the DOM in a web hosting environment

Note: Hosting Environment: Web browser, Node (server-side Javacript platform), and Adobe Flash

Document Object Model (DOM): Document Object model. Map the entire page to a multi-tiered node structure. Each component of an HTML or XML page is a node of a certain type, and the node contains different types of data. With the API provided by the DOM, developers can easily delete, add, replace, and modify any node.

Browser Object Model (BOM): support for accessing and manipulating browser windows

Chapter II using JavaScript in HTMLone, the way of quoting

1, Internal reference:

<script type= "Text/javacript" ></script>

Note: You only need to specify the Type property

2. External references:

<script type= "Text/javascript" src= "Example.js" ></script>
second, the location of the label

The code inside the <script> element will be interpreted first and foremost. The rest of the page will not be loaded or displayed by the browser until all the code inside the <script> element is evaluated by the browser. All generally put in before </body> load.

third, deferred scripts and asynchronous scripts

1. Delay script: The Defer property indicates that the script executes without affecting the construction of the page, and the script is deferred until the entire page has been parsed before running

<script type= "Text/javacript" defer= "defer" src= "Exaple1.js" ></script>

Note:

(1), only for referencing external files.

(2), the HTML5 specification requires scripts to be executed in the order in which they appear, so that when there are two delay scripts, the first delay script takes precedence over the second deferred script execution, which takes precedence over the domcontentloaded time before the trigger is executed

2, asynchronous script: Async does not allow the page to wait for two scripts to download and execute, thus asynchronously loading the page other internal

Note:

(1), only for referencing external files

(2), with defer no, if there are two script files, the second script file may be executed before the first script file. Asynchronous scripts must be executed before the Load event of the page, but may be executed before or after the domcontentloaded event is triggered.

Summary:

(1) All <script> elements will be parsed according to the order in which they appear in the page. If you do not use the defer and async attributes, the code in the following <script> elements will be parsed only after the code of the preceding <script> elements has been parsed

(2) Using the Defer property allows the script to be executed after the document is fully rendered. Deferred scripts are always executed in the order in which they are specified

(3) The Async attribute can be used to indicate that the current script does not have to wait for other scripts or to block document rendering. There is no guarantee that asynchronous scripts will execute in the order they appear in the page.

(4) Use the <noscript> element to specify that alternative content be displayed in browsers that do not support scripting. However, when scripting is enabled, the browser does not display anything in the <noscript> element.

Iv. Browser Load order

As the page loads, by default, the browser downloads, parses, and executes each script sequentially, in the order in which the script appears in HTML. During the process of processing a script, the browser neither downloads the contents of the script element nor renders it. This becomes blocking behavior.

Chapter III Basic Conceptsfirst, case-sensitive

Everything in ECMAScript (variables, function names, and operators) are case-sensitive

ii. identifiers (variables, functions, names of properties, parameters of functions)

1. The first character must be a letter, an underscore (-), or a dollar sign ($)

2. Other characters can be letters, underscores, dollar signs, or numbers

3, using the Hump case format: The first letter lowercase, the first letter of the remaining each word capitalized

Third, comments

1, single-line comment://

2, multi-line Comment:

/**/
Four, Strict mode

A different parsing and execution model is defined for Javacript. Under the strict model, some indeterminate behavior will be handled, and unsafe operations will throw an error. To enable strict mode throughout the script, add the following code at the top: "Use strict" (equivalent to compiling hints)

This compilation hint is included above the function, and can also be executed in the function strict mode

function dosomething () {  "use strict";   // function Body }
v. Statements

ECMAScript the statement ends with a semicolon, or if the semicolon is omitted, the parser determines the end of the statement. Do not omit;

vi. keywords and reserved words

1. Key words:

Break, do, instanceof, typeof, Case, else, New, Var, catch, finally, return, void, continue, for, switch, while, debugger, function, this, with, default, if, throw, delete, in, try

2. Reserved words:

Abstract, enum,int,Short,boolean, export, interface, static,byte, extends,  Long, Super,Char, final, native, synchronized, class,float, package, throws, const, Goto, private , transient,debugger, implements, protected, volatile,double, import, public, let, yield

3. Other

Eval, arguments
Seven, variable

Definition: The ECMA variable is loosely typed and can be used to hold any type of data. Each variable is simply a placeholder for holding the value.

Grammar:

1, uninitialized variables, the variable can hold any value. The system assigns a value to message by default undefined

VAR message;

2. This initialization variable does not mark it as a string type. The process of initializing is assigning a value to a variable. Therefore, you can modify the value's type while modifying the value of the variable.

var message= "HI";

3. A statement defines multiple variables

var message= "Hi",    found=false,    age=29;

Attention:

The variable defined with VAR is a local variable, and the variable defined by the Var is a global variable (deprecated)

viii. Types of data

Ecmascript:5 simple data types: Undefined, Null, Boolean, number, and string,1 complex data types: Object

1, the undefined:underfined type has only one value, it itself. When declaring a variable uninitialized, the value of the variable is undefined. Introduction Purpose: This object is introduced to formally distinguish between null object pointers and uninitialized pointers. TypeOf is detected as a declaration, or a variable that has not been assigned after the declaration is returned is undefined

2, NULL: null object pointer, if the defined variable is prepared to be used in the future to save the object, it is better to initialize the variable to Null instead of the other value. This way, you can know whether a reference to an object has been saved by the corresponding object as long as you check the null value directly.

var car = Null;alert (typeof car), typeof detects null value returns "Object"

Undefined, NULL difference: The Undefined value is derived from NULL, although null and Undefined have such a relationship, but their purpose is completely different. As long as the variable that is intended to hold the object does not actually hold the object, you should explicitly let the variable hold the null value.

3. Boolean type

The type has only two literals: true and false. But true does not necessarily equal 1, and flase does not necessarily equal 0. the system automatically turns when the flow control statement is executed. Force Reload : Boolean (). Note: True and false are not Boolean values, just identifiers

            conversion rules:

Data type

True

False

Boolean

True

flase

String

Any non-empty string

"" (empty string)

number

Any non-0 numeric value

0 and nan

Object

Any object

Null

Undefined

Not available

Undefined

4. Type of number

(1) Data type of the number:

Integers: In arithmetic operations, all octal and decimal values are eventually converted to decimal

var = intnum;  //   decimal:var octalNum1 = 079;  // octal: Starting            with 0 var // 16 binary: Start         with 0x

Floating-point number: This value must contain a decimal point, and must have at least one number after the decimal, since saving the floating-point value requires twice times the memory space to hold the integer value, so ECMAScript will lose no time in converting the floating-point value to an integer value.

var floatNum1 = 1.1;

(2) numeric conversion

There are three functions that convert non-numeric values to numeric values: Number (), parseint (), parsefloat (). Number (): Applies to any data type. parseint (), parsefloat (): specifically converts a string to a numeric value

5, NaN (not a number): This value is used to denote a case where an operand that would have returned a numeric value did not return a numeric value.

Characteristics:
Any operation that involves Nan will return Nan

Nan is not equal to any value. Including Nan itself

IsNaN (): Determines whether this parameter is "not a numeric value". After you receive the parameter, you try to convert the value to a number. Some values that are not numeric are converted directly to numbers. For example, the string "10" or the Boolean

6. String type

(1) said:

var firstName = "Nichoas";  // double quote "" var lastname= ' Zakas ';  // single quote "'

(2), string surface amount

Literal quantity

Meaning

\ n

Line break

\ t

Tab

\b

Backspace

\ r

Enter

\f

Paper Feed

\\

Slash Slash

\‘

Single quote '

\"

Double quotes "

\xnn

A character is represented by a hexadecimal code NN. \x41

Indicates "A"

\unnnn

A Unicode character represented by the hexadecimal code nnnn

(3), features: strings are immutable, that is, strings are created, their values can not be changed. To change the string saved by a variable, first destroy the original string, and then populate the variable with another string that contains the new worth.

(4), commonly used properties and methods

Property: Length Property: Calculates the lengths of a string

Method:

ToString () values, Boolean values, objects, and string values all contain this method. However, the null and undefined values do not have this method. In most cases, calling the Tostringi () method does not have to pass parameters. However, when you call the ToString () method of a number, you can pass a parameter: the cardinality of the output value. By default, the ToString () method returns a string representation in decimal format. By passing the cardinality, toString () can output binary, octal, decimal, and even any other valid binary

var num = ten; alert (num.tostring ());  Default decimal //  "Ten"alert (num.tostring (2));  // "1010" alert (num.tostring (8));  // " A" alert (num.tostring (//"alert" (num.tostring (//"a") 

String (): If you do not know if the value to be converted is null and undefined, you can also use the Transform function string (), which can convert any type of value to a string.

7. Type of Object

Definition: An object is actually a set of data and functions. An object can be created by executing the new operator followed by the name of the object type to be created. Create an instance of an object and add properties and methods to it

Cases:

Eight, operator

Arithmetic operators, bitwise operators, relational operators, and equality operators. They can use many values, such as strings, numeric values, Boolean values, and even objects. When applied to an object, the corresponding operator usually calls the object's valueof () method and/or the toString () method

Chapter fourth variables, scopes, and memory issuesa JavaScript variable can be used to hold two types of values:

1. Basic data type: undifined, Null, Boolean, number, String

2. Reference Object type

Characteristics:

The base type value occupies a fixed amount of space in memory and is stored in the stack memory .

The value of a reference type is an object, saved in heap memory

Copying a base type value from one variable to another creates a copy of the value

Contains a reference type the worthy variable actually contains not the object itself, but a pointer to the object

The value of the reference type is copied from one variable to another, and the pointer is copied, so two variables end up pointing to the same object

Determine if a value is that base type you can use the TypeOf operator, and determine which reference type a value is that can use the instanceof operator.

second, the execution environment of variables

All variables have an execution environment (also known as a scope) that determines the life cycle of the variable and which part of the code can access the variables.

1, the environment has the Global Execution Environment (global environment) and function execution environment points;

2. Each time you enter a new execution environment, you will create a scope chain for searching for variables and functions.

3, the local environment of the function not only has access to the variables in the scope of the function, but also has access to the parent environment and even the global environment;

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

third, JavaScript is a programming language with an automatic garbage collection mechanism, developers do not have to worry about memory allocation and recovery issues.

1. Values leaving the scope are automatically marked as recyclable and therefore will be deleted during garbage collection

2, "Mark Clear" is the current mainstream garbage collection algorithm, to the currently unused value tag, and then reclaim its memory

3. The reference count trace records the number of times that all values are referenced. The JavaScript engine is no longer using this algorithm, but this algorithm still causes problems when accessing non-native Javascipt objects in IE

4. When there is a cyclic reference object in the current code, the "reference counting" algorithm can cause problems.

5, the release of the reference to the variable not only helps to eliminate the phenomenon of circular reference, but also for garbage collection benefits. To ensure that memory is effectively reclaimed, you should release references to global objects, global object properties, and circular reference variables that are no longer in use in a timely manner

"JavaScript advanced Programming" reading notes

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.