“This is the third day of my participation in the August More Text Challenge.

Js data type

In JS, data types are divided into: original data type and reference data type.

  • Primitive data types: The values themselves cannot be changed. they are also called primitive values.

    The original value value The result of the typeof conversion
    Number 13 'number'
    String 'str' 'string'
    Boolean true or fasle 'boolean'
    Null On behalf of the empty 'object'
    Undefined No value assigned after name 'undefined'
    Symbol Symbol(123) 'symbol'
    BigInt 123n 'bigint'
  • Reference data types Reference values are accessed by address, and reference values can have properties and methods.

    Reference value value The result of the typeof conversion
    Object {} 'object'
    Array [] 'object'
    Date Mon Aug 23 2021 11:25:58 GMT+0800 'object'

Var, let, const

  • var
    • Contaminate global variables, usedvarNamed variables will be mounted globally
    • There will be variable promotion
    • Allow duplicate declarations
  • let
    • Does not pollute global variables
    • There will be no variable promotion
    • Duplicate declarations are not allowed
  • const
    • containsletAll the characteristics of
    • Make sure that the variable is assigned immediately upon initialization, otherwise an error will be reported
    • The value of this variable cannot be changed after assignment (the reference value within the reference value can be modified)
    • Loop variables cannot be usedconst

Differences between Typeof and Instanceof

Both typeof and instanceof can help us determine what typeof data this is, but the usage scenarios are different. Typeof is usually used to determine which data type is in the original value. Once used in reference values, all returned results are Object, which makes it difficult to determine. That’s where instanceof comes in handy. As follows:

let obj = {};
let arr = [];

console.log(typeof obj); // object
console.log(typeof arr); // object

console.log(obj instanceof Object); // true
console.log(obj instanceof Array); // false
console.log(arr instanceof Object); // true
console.log(arr instanceof Array); // true
Copy the code

Instanceof is whether the constructor’s prototype appears on an instance object’s prototype. So the result of arr instanceof Object is true. If you want to check whether the data is an array, you can try using the following methods:

let arr = [];
1. console.log(arr instanceof Object); // false
2. Array.isArray(arr); // true
Copy the code

scope

Before ES6, JS had only global scope and function scope.

  • Global scope: The global scope is created when the page is open and destroyed when the page is closed. Variables in the global scope can be used anywhere.

    What data is in the global scope?

    1. windowAll attributes in
    2. Variables assigned without declaration are mounted to global objectswindow
    3. Functions and variables declared globally

    As follows:

    var demo = 'demo';
    function a() {};
    c = 'value_c';
    Copy the code
  • Function scope: Variables and methods declared inside a function, and methods and variables in a function scope are not accessible externally, but variables and methods in a function scope are accessible externally. Imagine that you can take money from your father, but your father can’t take money from you.

    let demo = '123';
    
    function a() {
        console.log(demo);
    }
    a(); / / 123
    
    / / = = = =
    
    function a() {
        function b(){
            var c = '123';
        }
        b();
        console.log(c);
    } 
    a(); // c is not defined
    Copy the code

With the advent of ES6, we also have block-level scopes.

  • Block-level scope:

    The block-level scope scope is{To start,}The end.

    When usingletorconstIf a variable is declared in a block-level scope, it cannot be accessed outside that scope. Also, variables that have already been declared in a block-level scope cannot be declared again. andBlock-level scopewithFunction scopeExternal variables and methods can be accessed, but internal variables and methods cannot be accessed.

Is there any way for an external to access an internal variable? That’s using closures.

closure

In simple terms, it’s a function or a variable that’s inside that’s returned to the outside, and that’s a closure. Closures give us access to variables in the scope of another function. In addition, closures can also help us solve some problems during the development process, as follows:

// No closures are used
for(var i = 0; i < 10; i ++){
    setTimeout(function () {
        console.log(i);
    }, 100)}// Output 10 10s

/ / = = = =
// Use closures
for(var i = 0; i < 10; i ++){
    (function (j) {
        setTimeout(function () {
            console.log(j);
        }, 100)
    })(i)
}
// Outputs 0 ~ 9
Copy the code

You can also create private variables

function demo() {
    var a = 123;
    // return function b
    return function b() { 
        return a; // treat a as the return value of function B}};var fun = demo();
console.log(fun()); / / 123;
console.log(a); // a is not defined;
Copy the code

While closures are powerful, they are generally avoided because of the potential for memory leaks. Closures cause the parent function scope of the return function to not be released.

Array to heavy

let arr = [1.1.2.3.4.5.2.4.3.4.5.1.6]; // The array to process

/ / the first
function deWeight(arr) {
    let obj = {}, arrs = [];
    for(let key in arr){
        obj[arr[key]] = arr[key];
    }
    for(let key in obj){
        arrs.push(obj[key]);
    }
    return arrs;
}
console.log(deWeight(arr)); // [1, 2, 3, 4, 5, 6]


/ / the second
console.log(new Set(arr));

// There are more methods, please write them by hand
Copy the code