Error Error object

1. Browsers come with four types of error objects

  1. SyntaxError: SyntaxError — usually a symbol written wrong somewhere

     while() {// No parentheses
    Copy the code
  2. ReferenceError — used without being created

     console.log(a) // A has not been created yet
    Copy the code
  3. TypeError: TypeError – used for a method or property that is not your own

     const str = 'asdd'
     str.pop() //pop is an array method
    Copy the code
  4. RangeError: RangeError – only encountered in some individual places

     const num = 123.456
     num.toFixed(101) // The number of digits reserved for decimals is between 0 and 100
    Copy the code

If an error occurs, an error will be reported, and the subsequent code will not run, so you need to handle the error. Sometimes we don’t want it to run after an error, but to give a prompt, we can use try…. catch….. grammar

 try{error code; }catch(err){execute only if an error is reported; }console.log("Subsequent code")
Copy the code
 const num = 123.456
 ​
 try{
     num.toFixed(101);
 }catch(err){    
     console.log('You're out of range.');
 }
 console.log("Subsequent code")
Copy the code

try… catch… Performance is so poor that almost all of the code in it is minimized — it can be branched instead

 const num = parseFloat(prompt("Please enter a number."));
 const d = parseInt(prompt("Please enter the number of decimals you want to save."));
 ​
 if(d >= 0 && d <= 100) {console.log(num.toFixed(d));
 }else{
     console.log("The reserved decimal number must be between 0 and 100.");
 }
Copy the code

2. Throw custom errors

 throw new Error("Custom error text"); // If an error is reported, subsequent code will be blocked
Copy the code

The Function object

1. Creation method

  1. A declarative way

    functionThe function name (Parameter,...){function body;returnResults; }Copy the code
  2. Direct quantity mode

    constThe function name =function(Parameter,...){function body;returnResults; }Copy the code
  3. Constructor mode

    When the function body is not fixed, it can be used to concatenate it with strings

    constThe function name =new Function(Parameter "1"."Parameter 2". ."Function body return result");
    Copy the code

2. Scope

  1. Global scope – can be used anywhere

  2. Function scope – members are only available internally when a function is called

    Use local first, then find global, no more error

    const a = 10
    
    function(){
        const b = 20
        console.log(a) / / 10
        console.log(b) / / 20
        console.log(c) / / an error
    }
    Copy the code

3. Hoist (hoist)

Before the program executes, the browser precompiles the variables declared by var and the set of functions declared by function to the top of the current scope, leaving the assignments where they are, the function being heavier than the variable, and the function under the variable. (As explained in the previous notes, I won’t post the code here.)

4. pass by value — an assignment between two variables

  1. If the type is original, the two copies are made to each other

  2. If it is a reference type, the two affect each other and use the same address value

    / / the original
    let a = 10
    const b = a
    console.log(a) / / 10
    console.log(b) / / 10
    a = 30
    console.log(a) / / 30
    console.log(b) / / 10
    
    
    // Reference type
    const obj = {a:10}
    const newObj = obj
    console.log(obj.a) / / 10
    console.log(newObj.a) / / 10
    
    
    obj.a = 20
    console.log(obj.a) / / 20
    console.log(newObj.a) // 20 -- Reference the same address, one change will change the other
    Copy the code

5, overloading

Overloading refers to the same function name, depending on the argument passed in, you can choose to execute the corresponding function call execution, but JS does not support overloading, JS does not allow multiple functions with the same name to exist, the last function will override all previous functions with the same name

Arguments Inside a function there is a arguments object that can only be used internally during a function call. The object is automatically created with an array of classes that takes all arguments passed in.

The arguments function can be used to determine what action should be performed based on its length, type, etc

 // Sum, pass a few parameters we can find the sum of several parameters
 function add(){
     let sum = 0
     for(let i = 0; i < arguments.length; i++) {
         sum += arguments[i]
     }
     return sum
 }
 ​
 add(1.2.3.4)  / / 10
 add(1.2.3.4.5)  / / 15
Copy the code

6. Anonymous functions

  1. Self-tuning of anonymous functions – can replace global scope

     (function(){
         const a = 10
         console.log(a) / / 10}) ()console.log(a) / / an error
    Copy the code
  2. Anonymous function callback: Pass an anonymous function as an argument to another function call

     arr.sort((a, b) = > a - b)
    Copy the code

    Which functions are callbacks?

    A: Anonymous functions, either auto-callback or callback

     xxx.onclick = function(){} // This is a callback
    Copy the code

\