JavaScript Advanced Programming Note 01 (Basic concept)

Source: Internet
Author: User
Tags script tag

1. Use JavaScript 1, <script> elements in HTML

<script> defines the following 6 properties:

Async: Optional, asynchronous download of external script files.

CharSet: optional, by specifying the character set of the code through the SRC attribute, most browsers ignore this value, so few people use

Language: Abandoned

SRC: Optional, address of external script

Type: optional, now not recommended "Text/javascript", considering the conventional and maximum browser compatibility, the current value of the Type property is still text/javascript, however, this property is not required, the default is not set value is text/ Javascript.

Until all code inside the <script> element is evaluated by the parser, the rest of the page will not be loaded or displayed by the browser.

As a rule, all script elements should be in the head element of the page, but this practice causes all JavaScript code to be downloaded, parsed, and executed before it can begin rendering the contents of the page, so there is no doubt that the browser has a noticeable delay in rendering the page. The browser window will be blank in the delay period, in order to avoid this problem, modern Web applications generally put all the JavaScript reference to the content of the page in the BODY element,

Lazy Loading :

Html4.0 defines the defer attribute for the script tag, which is used to indicate that the footstep does not affect the construction of the page when it is executed, that the footstep is delayed until the entire page has been parsed and then run.

The HTML5 specification requires scripts to be installed in the order in which they appear, single in reality, deferred scripts do not necessarily install sequentially, so it is best to include only one deferred script, ie4, firefox3.5, Safari 5, and Chorme are the first browsers that support defer properties. Other browsers ignore this property and process the script as usual, so putting the deferred script at the bottom of the page is still the best option.

Asynchronous Script :

HTML5 defines the async attribute for the script element, which is asynchronous loading, and of course the disadvantage is the same as an AJAX asynchronous request, you cannot determine the order in which the scripts are executed. Asynchronous scripts are bound to run before the load time of the page is executed, and the order may be domconrentloaded (the difference between onload and jquery ready) before or after the event is triggered, and the browser that supports the asynchronous script has firefox3.6, Safari5 and Chrome.

embed code with external files :

Embedding JavaScript code in HTML is not a problem, but it is generally thought that the best practice is to use external files to contain JavaScript code as much as possible, but there is no hard rule to use external files (or to see specific actual projects, JavaScript code is not a lot of changes to the embedded page impact is not very large, and now the computer hardware equipment is getting better, the performance of the consumption is very small, and the embedded also saves the request, so there is no need to be required to refer to external documents, the last interview, The interviewer hears me a project to use is embedded immediately show contempt is attitude, I just want to say with what way to see the focus of the project, casually despise is your character has problems, but support the use of external files of people more emphasis on the following advantages:

    • maintainability: JavaScript across different HTML pages can cause maintenance problems, but putting all JavaScript files in one folder is much easier to maintain.
    • Cacheable: The browser is able to cache all external JavaScript files that are linked according to the specific settings.
    • Adapting to the future: include JavaScript through external files without using the previously mentioned XHTML or comment back.

Document Mode :

Ie5 introduces the concept of document mode: This concept is implemented by using document type switching, with the first two document modes: Promiscuous mode (quirks mode) and standard mode (standards modes). Promiscuous mode makes IE behave the same as IE5 (including non-standard features), while the standard mode allows IE to behave more closely to standard behavior. Although these two patterns mainly affect the rendering of CSS content, in some cases it will also affect the interpretation of JavaScript execution.

After the introduction of the concept of the document mode in IE, other browsers have followed suit, and after that, IE proposes a so-called quasi-standard mode (almost standards mode). There are many browser features in this mode are standard, single is not true, the non-standard place is mainly reflected in the processing of the picture gap (when using the picture in the table is the most obvious problem).

If the document type declaration is not found at the beginning of the document, then all browsers will turn on promiscuous mode by default, and it is not recommended to use promiscuous mode, because different browsers behave differently in this mode, and if you do not use hack (that is, some CSS property initialization assignment) technology, Cross-browser behavior is not consistent at all.

Code...

<noscript> element : This is rarely used, it is not recorded, interested can go to the JavaScript Advanced Programming design third Edition to see.

2. Basic Concepts

(some basic simple syntax I'm not here to record, just record some of the things I think important points of knowledge)

Strict mode:

ES5 (ECMAScript 5) introduces the strict pattern concept, which defines a different parsing and execution model for JavaScript, in strict mode, some of the indeterminate behavior in ES3 will be handled, and some unsafe operations will throw an error, To enable strict mode throughout the script, you can add the following code to the top:

"Use strict";

It is a compilation instruction for the high-speed supported JavaScript engine to switch to strict mode.

In strict mode, JavaScript executes very differently, so the book will always point out the differences in strict mode, and browsers that support strict mode include ie10+,firefox4+, safari5+, opera 12+, and Chrome.

data type : 5 data types in ES (also known as base data types): Undefined, null, Boolean, number, string, and Object,object are essentially composed of a set of unordered name-value pairs, ES does not support any mechanism for creating custom types, and all values will eventually be one of the 6 data types mentioned above.

Typeof operator

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

"Undefined": Undefined type

"Boolean": Boolean type

"string": String type

"Number": Numeric type

"Function": Functions

' Object ': Object or null

Undefined type

The undefined type is just a value, and when you use VAR to declare a variable but not initialize it, the value of the variable is undefined

Note: Executing the fypeof operator on an uninitialized variable returns the undefined value, while executing the typeof operator on an undeclared variable also returns the undefined value, as in the following code:

Var message;

Alert (typeof message); "Undefined"

Alert (typeof age);//"Undefined"

Null Type:

Null represents an empty object pointer that returns "Object" when NULL is detected using typeof.

If the defined variable is to be used for saving the object in the future, it is better to initialize the variable to null instead of the other value, so as long as you check the null value directly to know whether the corresponding variable has saved a reference to an object (but the individual feels that it can also be uninitialized, because in the judgment null== Undefined, so the null and undefined used to check are the same as the basic one).

Boolean type

Note: boolean literal values True and false are case-sensitive, that is, true and false are not Boolean values.

Although there are only two literal literals in the Boolean type, all types in ES have values that are equivalent to these two Boolean values, and you can call the Boolean () function transformation.

Data type

Value converted to true

Converted to False value

Boolean

True

False

string

Any non-empty string

empty string

number

Any non-0 numeric value

td>

0 and nan

Object

Any object

Null

Undefined

 

Undefined

Number type

    1. NaN

Nan, that is, a non-numeric value is a special value, which is used to indicate that an operand that would have returned a numeric value does not return a numeric value, Nan itself has two unusual characteristics, first: any operation involving Nan will return Nan, and the second: Nan is not equal to any value.

For the two characteristics of Nan, ES defines the isNaN () function, which takes a parameter that can be any type, and the function helps us determine if the parameter is "not a value". IsNaN () after receiving a value will try to convert the value to a number, in the detection.

    1. numeric conversions

There are 3 functions to convert non-numeric values to numeric values: Number (), parseint (), and parsefloat ()

Number () conversion rules for functions:

    • Can receive any type parameter
    • If it is a Boolean value, True and false are converted to 1 and 0;
    • If it is a numeric value, it is simply passed in and returned;
    • If it is a null value, returns 0;
    • If it is undefined, return nan;
    • If it is a string, follow these rules:
    1. If the string contains only numbers, it is converted to a decimal value;
    2. If the string contains a valid floating-point format, it is converted to the corresponding floating-point value;
    3. If the string contains a valid hexadecimal format, it is converted to a decimal value of the same size;
    4. If the string is empty, it is converted to 0
    5. If the string contains characters other than the above, it is converted to Nan;
    • If it is an object, the object's valueof () method is called, then the returned value is converted according to the preceding rule, and if the result of the conversion is Nan, the ToString () method of the object is called, and then the returned string value is converted again according to the previous rule;

JavaScript Advanced Programming Note 01 (Basic concept)

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.