"JavaScript Advanced Programming Notes" chapter I and chapter III

Source: Internet
Author: User
Tags hex code hasownproperty

1th JavaScript Introduction 1.2Javascript implementation

A complete JavaScript implementation consists of the following three different parts

Core (ECMAScript) provides core language capabilities

Document Object Model (DOM) provides methods and interfaces for accessing and manipulating Web page content

The browser object Model (BOM) provides methods and interfaces for interacting with the browser

ECMAScript

It prescribes the following components of the language:

Syntax Type statement keyword reserved word operator object

ECMA-262 5th Edition, published on 2009.

Document Object Model (DOM)

Document Object Model

World Wide Web Consortium WWW Alliance Planning DOM

DOM level

DOM1 (DOM Level1) October 1998 becomes the World Wide recommendation standard

The DOM1 level consists of two modules: the DOM core (DOM cores) and the DOM HTML

The DOM core rules how to map XML-based document structures, and Dom HTML expands on the DOM core, adding objects and methods for HTML.

The DOM2 level expands the mouse and user interface events, scopes, and traversal (methods of iterating DOM documents) on the original DOM, adding support for the CSS through the object interface.

The DOM2 class introduces the following new modules, as well as the definitions from the many new types and new interfaces.

Dom View (DOM views): Defines an interface that tracks different documents (for example, documents before and after applying CSS);

DOM Event (DOM events): An interface that defines event and event handling

Dom Style: Defines an interface that applies styles to elements based on CSS;

DOM Traversal and range (DOM traversal and Range): Defines the interfaces that traverse and manipulate the document tree.

The DOM3 class extends the DOM further, introducing a way to load and save documents in a uniform way – defined in DOM load and save (DOM load and save) modules, and new methods for validating documents – defined in the DOM validation (DOM Validation) module. The DOM3 class also extends the DOM core and begins to support the XML 1.0 specification, involving XML Infoset, XPath, and XML Base.

in Reading DOM Standard, the reader may see DOM0 level ( DOM Level 0 ) words. In fact,the DOM0 level standard is nonexistent; the so-called DOM0 level is just A reference point in the DOM's historical coordinates. Specifically, The DOM0 level refers to Internet Explorer 4.0 and Netscape Navigator 4.0 The DHTML that was originally supported .

Other DOM standards

For self-dom standards only, XML-based

SVG (Scalable Vector Graphic Scalable vectors) 1.0

MathML (mathematical Markup Language, Mathematical Markup Language) 1.0

SMIL (Synchronized Multimedia Integration language, Synchronized Multimedia integrated language)

Browser object Model (BOM)

BOM Browser Object Model

BOM extension:

Popup new browser window function;

The ability to move, bloom, and close the browser window;

A Navigator object that provides detailed browser information;

A Location object that provides detailed information about the page loaded by the browser;

Screen object that provides detailed information about the user's display resolution

Support for Cookies

Custom objects such as the activexobject of XMLHttpRequest and IE

The 2nd Chapter uses javascript2.1 <script> elements in HTML

HTML4.01 defines 6 properties for <script>:

Async: Optional. Indicates that the script should be downloaded immediately, but does not interfere with other actions on the page, such as downloading additional resources or waiting for other scripts to load. Valid only for external script files.

Charset: Optional. Represents the character set of the code through the SRC attribute. Because most browsers ignore its value, this property is rarely used.

Defer: Optional. Indicates that the script can be deferred until the document is fully parsed and displayed before execution. Valid only for external script files. Ie7-also supports this property for embedded scripting.

Language: Deprecated.

SRC: Optional. Represents the external file that contains the code to execute.

Type: Optional. Not required, default is: Text/javascript is also called MIME type.

The following values can be used in non-IE browsers: Application/javascript and Application/ecmascript

2.2 Embed code with external files

Advantages of using external files:

maintainability: JavaScript across different HTML pages can cause maintenance problems.

Cacheable: The browser is able to cache the linked external JavaScript files according to the specific settings. If you have two pages that use the same file, this file needs to be downloaded only once. The end result is the ability to speed up page loading.

Adapting to the future: using an external file to contain Javascipt does not require XHTML or annotation hack. The syntax for HTML and XHTML to contain external files is the same.

2.3 Document Mode

IE5.5 introduces the concept of document mode, which is implemented through a full-use document type (DOCTYPE) switch. The first two document modes are: promiscuous mode (quirks modes) and Standard mode (standards modes).

These two modes primarily affect the rendering of CSS content and, in some cases, the execution of Javascipt.

If the document type declaration is not found at the beginning of the document, all browsers turn on promiscuous mode by default. But using promiscuous mode is not a recommended practice, because different browsers behave differently in this mode, and if you don't use some hack technology, cross-browser behavior is not consistent at all.

Standard mode, which may be enabled by using any of the following document types:

<!-html 4.01 Strict--

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en"

"Http://www.w3.org/TR/html4/strict.dtd" >

<!-html 1.0--

<! DOCTYPE HTML Public

"-//w3c//dtd HTML 1.0 strict//en"

"Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >

<!--HTML 5--

<! DOCTYPE html>

Chapter 3rd Basic Concepts

ECMAScript 5 introduces the concept of strict mode (strict modes).

To enable strict mode, add the following code at the top:

"Use strict";

It is a compilation instruction (Pragama) that tells the supported JavaScript engine to switch to strict mode. A specially selected syntax for not destroying ECMAScript 3 syntax.

Include this rule instruction above the function content, or you can specify that the function is executed in strict mode:

Function dosomething () {

"Use strict";

}

3.2 keywords and reserved words

Key words

Break does instanceof typeof case Else new Var catch finally return void Continue for switch While debugger function this with default if throw Dele Te in try

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 Doble import public

Using keywords as identifiers in the Javascrpit engine can cause "Identifier expected" errors.

3.3 Variables

Var Declaration

Massge = "HI"

Assigning values to undeclared variables throws a referenceerror error in strict mode

3.4 Data types

ECMAScript has five simple data types (also known as basic data types): undefined,null,number,boolean,string

There is also a complex data type: Object.

3.4.1 typeof operator

Using the typeof operator may return one of the following strings:

"Undefined": if this value is undefined

"Boolean": If this value is a Boolean value

"String": If this value is a string

"Number": If this value is numeric

"Object": If this value is an object or null

"Function": If this value is a function

3.4.4 Boolean type

The type has only two literals: true and false.

True does not necessarily equal 1, and false is not necessarily equal to 0.

To convert a value to its corresponding Boolean value, you can call the transform function, Boolean (), as follows:

Var massage = "Hello world";

Var Massageasboolean = Boolean (massage);

The string message is converted to a Boolean value.

Data type

The value converted to true

Value converted to False

Boolean

True

False

String

Any non-empty string

"" (empty string)

Number

Any non-0 numeric value (including infinity)

0 and Nan

Object

Any object

Null

Undefined

N/A

Undefined

3.4.5 Unmber Type

Floating-point numbers are called double-precision values in some languages.

Decimal

Octal (base 8): The first bit must be zero. is not valid in strict mode

Hexadecimal (base 16): The first two bits must be 0x, followed by any 16-in digits (0~9 and a~f). Where A-Z can be capitalized or lowercase.

The highest precision for floating-point values is 17 decimal places.

0.1+0.2=0.3000000000000004

2 Range of values

Number.min_value

Number.MAX_VALUE

Infinity positive Infinity-infinity negative infinity

Determine if a number is poor and use the isfinite () function. Returns true between the maximum and minimum values;

Access to number.negative_infinity and number.positive_infinity can be a negative and positive INFINITY value.

Nan, non-numeric (not a number), and Nan is not equal to any value, including Nan itself.

ECMAScript defines the isNaN () function.

2 Numeric conversions

There are three functions that can convert a non-numeric value to a value:

Number (): Can be used with any data type

Boolean:true and false will be converted to 1 and 0 respectively;

Number: Simple to pass in and return.

Null: returns 0.

Undefined: Return nan

String, followed by the following rules:

    1. Contains only numbers (including cases preceded with a plus or minus sign), it is converted to a decimal value, "1" = 1 "123" = 123 "011" =11
    2. Floating-point format, which converts the corresponding floating-point value, ignoring the leading 0
    3. Valid hexadecimal converts it to a decimal integer of the same size
    4. Null character (without any characters) converted to 0
    5. Characters other than the above format, converted to Nan

object, follow these rules:

    1. Call the ValueOf () method, and then convert according to the previous rule
    2. If the result of the conversion is Nan, call the ToString () method of the object, and then convert it according to the preceding rule

parseint (): Designed to convert strings into numerical values

Converting an empty string returns Nan

Parsefloat (): Designed to convert strings into numerical values

3.4.6 String Type

Character literal

Literal quantity

Meaning

\ n

Line break

\ t

Tab

\b

Space

\ r

Enter

\f

Paper Feed

\\

Slash Slash

\’

Single quotation marks

\”

Double quotes

\xnn

The hexadecimal code, such as \x41, indicates a

\unnnn

Hex code such as \U03A3

Numeric, Boolean, object, and string values have the ToString () method, but the null.undefined value does not have this method.

In most cases, calling the ToString () method does not have to pass arguments. You can pass a parameter on invocation: The cardinality of the output value. The string representation of a numeric value is returned by default in decimal format. By passing the cardinality, you can output a string in binary, octal, hexadecimal, and any other valid binary format representation.

3.4.7 Object Type

Each instance of object has the following properties and methods.

Constructor: Holds the function that is used to create the current object. The constructor (constructor) is an object.

hasOwnProperty (propropertyname): Used to check whether a given property exists in the current object instance, not in the instance prototype. The attribute name (PropertyName) as a parameter must be specified as a string (for example: O.hasownproperty ("name");

isPrototypeOf (object): Used to check if an incoming object is a prototype of another object.

propertyIsEnumerable (PropertyName): Used to check whether a given property can be enumerated using the For-in statement. As with the hasOwnProperty () method, the property name as a parameter must be specified as a string.

Tolocalstring (): Returns the string representation of the object that corresponds to the region where the execution environment is executed.

Tosting (): Returns the string representation of the object.

ValueOf (): Returns the string, numeric, or Boolean representation of an object, usually the same as the return value of the ToString () method.

object is the basis of all objects, so all objects have these basic properties and methods.

Both the BOM and the objects in the DOM belong to the array object.

3.5 operator

Bitwise operator does not understand

Logical NON (! )

Logic and (&&)

Number of first operand

Second operand

Results

True

True

True

True

False

False

False

True

False

False

False

False

Logic or (| | )

Number of first operand

Second operand

Results

True

True

True

True

False

True

False

True

True

False

False

False

3.6.2 do-while Statements

var i=0;

do{

I +=2

}while (I<10); 10

As long as the value of I is less than 10, the loop will go on.

3.6.2 while statement

var i=0;

while (i<10) {i + = 2};

As long as the value of I is less than 10, the loop will go on.

3.6.6 Label Statement

Start:for (Var i=0;i<count;i++) {}

The start tag defined in this example can be referenced by a break or continue statement. Tagged statements are generally used in conjunction with loop statements such as for statements

3.6.7 Break and Continue statements

The break and continue statements are used to precisely control the execution of code in a loop.

The break statement exits the loop immediately, forcing the statement following the loop to continue.

The continue statement exits the loop immediately, but exits the loop and resumes execution from the top of the loop.

3.6.7 with statements

The function of the WITH statement is to set the scope of the code to a specific object.

The purpose of defining the with statement is primarily to simplify the work of writing the same object multiple times.

Var qs = location.search.substring (1);

Var hostname = Location.hostname;

Var url = location.href;

The previous lines of code contain the Location object. If you use the WITH statement, you can rewrite the above code as follows:

With (location) {

Var qs = search.substing (1);

Var hostname = hostname;

Var URL =href;

}

The WITH statement is not allowed in strict mode, otherwise it is considered a syntax error.

The use of with statements in large numbers can cause performance degradation and also make debugging code difficult, so it is not recommended to use the WITH statement when developing large applications.

3.6.8 Switch statement

Switch (expression) {

Case Value:dosomething

break;

Case Value:dosomething

break;

Default:dosomething

}

"JavaScript Advanced Programming Notes" chapter I and chapter III

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.