I- Chapter 1 types

The specificity of one person and the specificity and wonder of one person do not overwhelm another. The genius in you will inspire the genius in me.

Built-in types

The ECMAScript standard defines eight data types:

Seven primitive types:

Undefined
Null
Number
Boolean
BigInt
String
SymbolObject(Basic type)Copy the code

Memory tip: You (O) (U) (2) (N) (B) (S)

Typeof is used to view the typeof the value, which returns a string. But types and their string values do not correspond

typeof undefined= = ='undefined'  // true
typeof true= = ='boolean' // true
typeof 12= = ='number' // true
typeof '12'= = ='string' // true
typeof { life: 12} = = ='object' // true
typeof Symbol() = = ='symbol' // true
typeof BigInt('1') = = ='bigint' // true
typeof Object(BigInt('2'= = =))'object' // true

// Not the same
typeof null= = ='object' // true

// Compound conditions are required to detect the type of null values

const a = null; (! a &&typeof a === 'object'); // true

// function is also a built-in JS type, which is actually a subtype of Object
typeof function a() {/ * * /} = = ='function'; // true
// Arrays are subtypes of object
typeof [1.2.3= = ='object' // true
Copy the code

Values and types

JS variables do not have types; values do. Variables can hold any type of value at any time.

The result of typeof is not the typeof the variable, but the typeof the value that the variable holds. It always returns a string.

let a = 12;
typeof a; // 'number'

a = true;
typeof a; // 'boolean'

typeof typeof 12; // 'string'
Copy the code

typeof Undeclared

let a;
a; // undefined
b; // ReferenceError: b is not defined

typeof a; // 'undefined'
typeof b; // 'undefined'
Copy the code

Typeof also returns undefined for undefined variables. No errors were reported.

This is because Typeof has a special security mechanism.

Multiple scripts load variables in a shared global namespace.

// This throws an error
if (DEBUG) {
    console.log( 'Debugging is starting' );
}
// This is safe
if (typeofDEBUG ! = ='undefined') {
    console.log( 'Debugging is starting' );
}

// Built-in apis are also helpful
if (typeof atob === 'undefined') {
    atob = function() { / *.. * / };
}
Copy the code

Typeof security can also be used to determine non-global variables

(function(){
    function FeatureXYZ() { / *.. my XYZ feature .. * / }
    / / contains doSomethingCool (..)
    function doSomethingCool() {
        var helper =
            (typeofFeatureXYZ ! = ='undefined')? FeatureXYZ :function() { / *.. default feature .. * / };
        var val = helper();
        // ..} doSomethingCool(); }) ();Copy the code

FeatureXYZ is not a global variable here, but can also be checked using typeof security precautions

Dependency injection can also be used

function doSomethingCool(FeatureXYZ) {
    var helper = FeatureXYZ ||
    function() { / *.. default feature .. * / };
    var val = helper();
    // ..
}
Copy the code

Type summary

  1. Learn about built-in JS types
  2. Determine the type according to typeof
  3. Typeof security mechanism application scenarios