This is the 7th day of my participation in the August More Text Challenge

preface

In the daily development and learning, I always meet some strange problems, so record down!

Basic syntax and variables

Continuously defined variable

In everyday development, we often use scenarios where multiple variables are defined

   var a=b=3
Copy the code

But that’s what happens in the JS interpreter’s eyes

   b=3
   var a=b
Copy the code

Since the = operator operates from right to left, b=3 is executed first, followed by var a=b

Var (let, count) {var (let, count);} var (let, count) {var (let, count);} var (let, count) {var (let, count);}

Here’s an example:

   function test(){
       var a=b=2
       console.log(a)  //2
       console.log(b)  //2
   }
   test()
   console.log(a)  // ReferenceError: a is not defined
   console.log("a" in window)  //false
   
   console.log(b)  // 2
   console.log("b" in window)  //true
Copy the code

Obviously, b is defined globally, so it will pollute the world, and this kind of error is hard to find. If you want to define multiple variables, separate them.

var a = 2,b = a; // Write it correctlyCopy the code

Var a = 2; var b=a; var b=a The JS interpreter is as follows:

   var a = 2;
   var b = a; 
Copy the code
Common spooky comparisons and operations
   null >= 0 //true
   null <= 0 //true
Copy the code

Combined with the above two expressions, we can derive null==0, but when we put it on the console, we find that this is not the case

   null == null  //false
Copy the code

The same comparison operator, why the inference is different?

This is because the implicit conversion of the JS== operator is not the same as >= or <=, so it is sometimes impossible to think of code mathematically

[] == [] //false [] == ! [] //falseCopy the code

These are some of the most difficult and tricky interview questions you’ll ever hear, but let’s get over them.

Comma operator

The, operator is the one we use most often, but it is also the one we ignore most, because no one uses the, operator directly, such as:

Let a= (1,2) // a=3 (1,2) + (6,2) // 4Copy the code

Although such statements are uncommon, it is important to know why the result is returned.

The value of a comma expression is the value after the last comma. For example, the (1,2) expression, then its value is 2

Comma operator expressions generally appear in compressed source code and interviews

The wrapper class of the basic type

What is a wrapper class

  var str="abc"
  console.log(str.length)  //3
  str.length = 6
  console.log(str.length)  //3
Copy the code

The length property of STR is changed, but the length property is still the same.

We can break down the statement to see what the JS interpreter does

  var str="abc"
  console.log(new String(str).length)  //3
  new String(str).length = 6
  console.log(new String(str).length)  //3
Copy the code

If you change the length property of the object, the next time you access the object, you will still be accessing a new object, so length

function

Self-executing functions
   function test(){
       console.log(1)
   }()  //SyntaxError: Unexpected token ')'
Copy the code

The above way of writing self-executing functions is bound to give an error, so I won’t explain it here.

Let’s see if the following ways will give you an error

   function(){
       console.log(1)
   }(1)      //1
Copy the code

As you can see, just adding a function parameter does not generate an error. Okay, so how does the JS interpreter interpret this line of code

   function(){
       console.log(1)
   }
   (1)
Copy the code

Since the () operator is high and contains data, it is not used as a function execution symbol

scope

There is no block-level scope
    function test(){
        if(true){
           var a=2
       }
       console.log(a)  //2
    }
   test()
Copy the code

Programmers who have worked in other languages may wonder when they first encounter this code: why is a variable accessible? Why is a variable defined inside an if statement block accessible outside of it?

The function scope and the global function scope exist in the language itself, so there is no block level scope in the language itself, so we can understand why the variables defined in the if statement block can access the LAN outside. Because the a variable is in the scope of the function generated by the test function, it can be accessed from anywhere inside the test function. Similarly, JS does not generate block-level scopes where for, while, and other languages generate block-level scopes.

This rule was changed after ES6 came out, but only let and count variables can generate block-level scope, and var variable scope rules remain the same.

Archetypes and inheritance

Append the return value to the constructor

In JS basics we know that the constructor will have a default return value, which is this for the current environment, but what happens when we customize the return value?

Function Test(){this.name= new Test() console.log(Test)} let Test =new Test() console.log(Test) // Test {name: "console.log "}Copy the code

As expected, our custom return value does not interfere with the value returned by the constructor or the newly created object, but consider the following case

Function Test(){this.name=" "return {name:" "}} let Test =new Test() console.log(Test) // {name: ""}Copy the code

Our custom return value replaces the constructor’s return value, which is completely different

Conclusion: When you customize the return value of a constructor, it does not interfere with the original return value of the constructor if the return value is of primitive type. However, if the custom return value is a reference type, it replaces the original return value of the constructor

Modify the stereotype object that the object inherits

 let pro={
     a:1
 }
 let test1=Object.create(pro)
 let test2=Object.create(pro)
 test1.a=999
 console.log(test1.a)   //999
 console.log(test2.a)   //1
Copy the code

Tese1 and test2 inherit from the same object. When test1 modiates A, test2’s A is not affected. This makes sense simply because test1 is not modified to pro’s A value

Let pro={a:[1,2,3]} let test1= object.create (pro) let test2= object.create (pro) test1.a.paush (5) console.log(test1.a) // [1, 2, 3, 5] console.log(test2.a) // [1, 2, 3, 5]Copy the code

Obviously a can be modified if it is a reference type, which is why it is recommended not to put attributes on prototype objects. Right

Continuously updated…