Learn the basics of front-end JAVASCRIPT-5 and JavaScript from scratch ES5

Source: Internet
Author: User

1:ES5 Introduction

ECMAScript 5.1 (or ES5 only) is a revision of the ECMAScript (JavaScript-based specification) standard. Similar to the HTML5 specification process, ES5 is standardized by merging existing JavaScript methods with the addition of statements and native ECMAScript objects.

2: The meaning of strict mode

Relative to Normal mode:

1: Eliminate some unreasonable and less rigorous JavaScript syntax, and reduce some of the bizarre behavior.

2: Eliminate some unsafe code to run, to ensure the security of code operation.

3: Improve the efficiency of the compiler, increase the speed of operation.

4: Pave the future for a new version of JavaScript.

"Strict mode" embodies JavaScript more reasonable, more secure, more rigorous development direction, including IE 10 mainstream browser, has supported it, many large projects have begun to embrace it fully.

3: Enter strict mode

Enter the "strict mode" flag and write this line of statement: "Use strict";

Older browsers will ignore it as a single line of normal strings.

4: How to call Strict mode

There are two methods of calling strict mode, which are suitable for different situations.

for the entire script file : Put "use strict" in the first line of the script file, the entire script will run in "strict mode." If the line statement is not in the first row, it is not valid and the entire script runs in normal mode. This requires special attention if the code files of different schemas are merged into one file.

for a single function : put "use strict" on the first line of the function, then the entire function runs in strict mode.

Workarounds for script files: because the first method of invocation is not conducive to file merging, it is better to borrow the second method and place the entire script file in an anonymous function that executes immediately.

eg

A.js (the JS code of the member a introduces strict mode)/*/' use strict '; var a = 4;var B = 3;console.log (A + b); *///b.js (JS code for Group B introduces normal mode)/*var c = 6;var d = 7;console.log (c + D); *///Project Line Code merge (if the code is merged directly, the generation of Group B does not coding according to the strict pattern specification, the syntax may have various problems). Solution: Encapsulate the code separately into two anonymous self-executing functions (function () {' Use strict '; var a = 4;var B = 3;console.log (A + b);}) ();(function () {var c = 6;var D = 7;console.log (c + D);}) ();
5: Strict mode changes syntax and behavior

Strict mode has made some changes to JavaScript's syntax and behavior.

1 : The global variable is explicitly declared.

In normal mode, if a variable is assigned without a declaration, the default is the global variable. Strict mode prohibits this usage, and global variables must be explicitly declared. Therefore, in strict mode, variables must first be declared with the Var command before they are used.

Variables in Javascrip the global variables defined inside the function without the keyword var if strict mode is introduced, the run program will check for syntax errors. Declare global variables with display.

2 : Function parameters do not allow duplicates.

3 : Prohibit the use of with statements.

4 : The This keyword inside the function is prohibited from pointing to the global object, this is pointing to undefined.

5 : The function must be declared at the top level.

A new version of JavaScript will introduce a "block-level scope" in the future. In order to conform to the new version, strict mode only allows functions to be declared at the top level of the global scope or function scope. That is, it is not allowed to declare a function within a block of code that is not a function.

if (1) {function A () {}} else {function A () {}}

Learn the basics of front-end JAVASCRIPT-5 and JavaScript from scratch ES5

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.