JavaScript Basic Concepts A

  • Introduction

If you need to understand these concepts, you should be familiar with the basics of JS.

  • weakly typed

Weakly typed languages ​​are relative to strongly typed languages. In strongly typed languages, there are many types of variables, such as int, char, float, boolean, etc. Different types of conversion sometimes require coercion, while javascript has only one type, var, which assigns values ​​to variables. When the type is automatically judged and converted, javascript is a weak language, which is reflected in the variable definition type var.

 

  • 'use strict' is often used;

This line of code looks like a string and isn't assigned to any variable, but it's actually a pragma that tells supporting JavaScript engines to switch to strict mode. This was deliberately chosen to not break ECMAScript 3 syntax.

Include this pragma above inside a function, or you can specify that the function executes in strict mode.

 

function () {
    'use strict';
    return a;
}

The above example will report an exception in strict mode, the program does not create a global variable a, but in non-strict mode it will quietly create a global variable a.

 

  • Semicolons are not optional

Statements in ECMAScript end with a semicolon; if the semicolon is omitted, the parser determines the end of the statement, as in the following example:

var sum = a + b // true, but not recommended
var diff = ab; // correct, k recommended

Although a semicolon at the end of a statement is not required, we recommend that it be omitted at any time.

  •  scope

Unlike C, C++ or Java, JS has only two simple scope types - global level and function level. So, in JS, if, if, while , for do not define scope blocks. code show as below:

function () {
    if (someCondition) {
        var a;
    }
}

is actually the same as the following code

function () {
    var a;
    if (someCondition) {
        // ...
    }
}

Also, variables are generally defined at the top.

function test() {
    'use strict';
    console.log(a);
    console.log(b);
    //console.log(x);
    var a = 10, b = 10;
    console.log(a);
    console.log(b);
}
test();

The output is as follows:

undefined
undefined
10
10

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325027511&siteId=291194637