First, basic knowledge

Var/let/const

1. Different scopes

Var: function scope

Var defines a variable that is scoped to the local variable that contains its function. The change is destroyed when the function exits. (So variables defined with var inside the function are not accessible outside the function)

Let and const: block-level scope

Variables defined by let and const are scoped to the block of code that contains them.

2. Variables declared by var are promoted. Variables declared by let and const are not

3. Var may declare a variable repeatedly. Let and const may declare a variable repeatedly.

4. Global variables declared as let or const do not become properties of the window object. Variables declared as var do

2.typeof

A typeof values The return value
Uninitialized, undeclared variable undefined
boolean boolean
string string
number number
The object, null, object
function object
symbol symbol

Typeof principle:

When js stores a variable in the underlying layer, it stores the type information 👉 in the low 1-3 bits of the variable’s machine code

000: object

010: floating point number

100: string

110: Boolean

1: the integer

But, for undefined and null, the information storage for these two values is a little special.

Null: All machine codes are 0

Undefined: represents −2^30 as an integer

Therefore, Typeof has a problem when judging null, since all the machine code of null is 0, it is directly treated as an object.

3. Instanceof principle

Every JavaScript object has an implicitprotoThe explicit stereotype property is prototype.instanceofThe idea is to take the prototype property on the right and check the __prop__ property on the left until it is equaltrueReturns until null is not foundfalse.

4. Numerical conversion

Explicit conversion

Convert non-numeric values to numeric values: Number(), ParseInt(), ParseFloat()

ParseInt() conversion rule: converts from the first non-space character, and returns naN until the end of the string if the first character is not a numeric value, + or -, so an empty string will also return naN.

ParseFloat() conversion rule: convert from the first non-space character, and if the first converted character is not a numeric value, + or – returns a naN until the end of the string, so an empty string will also return a naN, and all subsequent ones will be ignored if only the first decimal point is valid.

function parameter Parameter –> Return value true/false The numerical null undefined String with value The value contains a number, +, and – The string contains floating point numbers The value contains hexadecimal digits An empty string None before the string
Number() Any data type 1/0 Direct return 0 naN Decimal number Decimal number Floating point Numbers A hexadecimal number 0 naN
ParseFloat() string 1/0 Direct return 0 naN Decimal number Decimal number Floating point Numbers A hexadecimal number 0 naN
### Implicit conversion

Replace with a string:

Explicit conversion

ToString (): With the exception of null and undefined, each value has a toString() attribute.

String() transformation function: null and undefined return “null” and “undefined”, other values call toString().

Implicit conversion

Use + to add a “” to a value

5. for.. And in the for… of

For in and for of

To compare for.. in for.. of
The difference between You can iterate over normal objects

Iterates through the prototype object of the array

You can iterate through the array itself

The value that comes out of the loop is key

Map /set cannot be traversed

Generators cannot be iterated

Internet explorer support
Normal objects cannot be traversed

It doesn’t iterate through the prototype object

It doesn’t iterate over its own properties

The value that comes out is value

You can iterate over a map/set

Can iterate generators

IE does not support
The same You can iterate through groups of numbers

You can break the traverse
You can iterate through groups of numbers

You can break the traverse

Two, handwritten implementation function

1.instanceof

function myInstanceof (left, right) {
           const RigPro = right.prototype
           while (true) {
               if (left === null) {
                   return false
               }
               if (left.__proto__ === RigPro) {
                   return true
               }
               left = left.__proto__
           }
       }
Copy the code

2. The new keyword

What the new keyword does:

1. Create a new object in memory

2. Assign the __proto__ property inside the new object to the constructor’s prototype property.

3. This inside the constructor is assigned to the new object (i.e. this refers to the new object)

4. Execute code inside the constructor (add properties to the new object)

5. If the constructor returns a non-empty object, return that object; Otherwise, the newly created object is returned.

function myNew (func, ... args) {
            // The myNew function takes two arguments: the constructor, and the arguments required by the constructor
            const newObj = {}
            newObj.__proto__ = func.prototype
            constobj = func.call(newObj, ... args)if((typeof obj == 'object' || typeof obj == 'function') &&typeofobj ! = ='null' ) {
                return obj
            }
            return newObj
        }
        function Person(name, age) {
            this.name = name
            this.age = age
        }
       const p1 =  myNew(Person, 'lili'.18)
       console.log(p1);
Copy the code

3. Call the function

Object.prototype.myCall = function (context, ... args) {
            // When context is null or undefined, assign context to window
            context = context || window
            // Create a new fn attribute to hold the current call to this
            context.fn = this
            constresult = context.fn(... args)delete context.fn
            return result
        }
        const p1 = {
            add: function add (a, b) {
                return a + b
            }
        }
        console.log(p1.add.myCall(p1, 1.2)); 
Copy the code