How do I determine the data type

1. Typeof operator

2. toString ( )

3. toLocalString

Detection array

1. instanceof

2. constructor

3. Array.isArray

Function throttling and anti – shake

1. The throttle

The core is the switch lock 🔒.

For example: click the button multiple times to submit the form (first time valid)

2. If you

Perform only the last asynchronous task that was triggered to clear the previous asynchronous task, the core of which is to clear the zero.

For example: page scroll processing event, search box input association, (last valid)

What exactly does the new operator do

  1. Create an empty object

  2. Inherits the prototype of the function

  3. Properties and methods are added to the object referenced by this

  4. The newly created object is referred to by this and implicitly returns this

Strict schema restrictions

  1. Variables must be declared before being used

  2. Function arguments cannot have attributes of the same name; otherwise, an error is reported

  3. The with statement cannot be used

  4. Cannot assign a value to a read-only attribute, otherwise an error is reported

  5. Octal numbers cannot be represented with the prefix 0, otherwise an error is reported

  6. You cannot delete attributes that cannot be deleted; otherwise, an error is reported

  7. Cannot delete variable delete prop, error will be reported, only delete property delete global[prop]

  8. Eval does not introduce variables in its outer scope

  9. Eval and arguments cannot be reassigned

  10. Arguments do not automatically reflect changes to function arguments

  11. Arguments.callee cannot be used

  12. Can’t use arguments.caller

  13. Disallow this from pointing to a global object

  14. You cannot use fn.caller and fn.arguments to get the stack of function calls

Var and let const

  1. Variables declared by var are mounted on the window, while variables declared by let and const are not
  2. Var declares variables with variable promotion, let and const do not (strictly speaking, let also exists)
  3. Let and const declarations form block scopes
  4. Let has a temporary dead zone
  5. Const declaration must be assigned
  6. Let and const cannot be declared twice in the same scope. Var can

Call apply and bind

Call Apply common points:

The object that calls call and apply must be a Function

Call: the Function call (obj, param1, param2,… ,paramN)

apply: Function.apply(obj, [argArray])

The bind() method creates a new function that sets the this keyword to the supplied value when called.

And when the new function is called, the given parameter list is taken as the first several items of the parameter sequence of the original function.

The main purpose of Call and Apply is to change the execution context of an object, and to do so immediately. They are written slightly differently in terms of parameters.

Bind can also change the execution context of an object; unlike Call and apply, the return value is a function that needs to be called later before it is executed.

The eval and with

eval(..) And with modify or create new scopes at run time to trick other lexical scopes defined at write time.

They seem to enable more complex functionality, and the code is more extensible.

eval(..) A function can take a string as an argument and treat its contents as if they existed at that point in the program at the time of writing.

In other words, you can programmatically generate code in the code you write and run it as if it were written in that location.

With can change scope and is often used as a shortcut to refer repeatedly to multiple properties in the same object without having to refer repeatedly to the object itself.

this

This points to:

In ES5: This always refers to the object that last called it

ES6: The arrow function’s this always points to this when the function is defined, not when it is executed

How to change the direction of this:

  1. Use the arrow function of ES6
  2. Use _this = this inside the function
  3. Use apply, call, and bind
  4. New instantiates an object

Scope and scope chain

Scope is the accessible scope of variables and functions, that is, scope controls the visibility and life cycle of variables and functions. Scope is a set of rules for variable lookups based on identifier names in the current scope as well as nested subscopes.

The function of scope chain is to ensure that the variables and functions that have access to the execution environment are in order. The variables of the scope chain can only be accessed up, and the access of variables to the window object is terminated, while the access of variables down the scope chain is not allowed.

The difference between arrow functions and ordinary functions

  1. The this object inside the function is the object at which it is defined, not used
  2. Should not be used as a constructor, that is, the new command should not be used, otherwise an error will be thrown
  3. You cannot use the Arguments object, which does not exist in the function body. If you do, use the Rest argument instead
  4. Yield cannot be used, so arrow functions cannot be used as Generator functions

Promise object

Promise is a solution to asynchronous programming that is more reasonable and powerful than traditional solutions (callback functions and events).

Chain operations are better than callbacks.

  1. The Promise constructor is executed synchronously. Then is asynchronous
  2. Once a promise status changes, it cannot be changed
  3. Either. Then or. Catch will return a new promise
  4. A promise’s.then or.catch can be called multiple times
  5. Return an error object in. Then or. Catch does not throw an error
  6. The value returned by. Then or. Catch cannot be the promise itself

Disadvantages:

There is no way to cancel a Promise, which is executed as soon as it is created and cannot be cancelled halfway through.

If the callback function is not set, errors thrown inside a Promise are not reflected outside.

When you are in a pending state, you have no way of knowing what stage of progress you are currently in (just started or about to complete).

The implementation of asynchronous programming

  1. The callback function
  2. Event listening (in time-driven mode, depending on whether an event occurs or not)
  3. Publish/subscribe (Observer mode)
  4. The Generator function
  5. Async function

Depth copy

Shallow copy: Only the first layer is copied

  1. Object. The assign,
  2. Expansion operator…

Parse (json.stringify (object))

Defect:

  1. Ignores undefined
  2. Ignore the symbol
  3. Non-serializable function
  4. Cannot resolve object referenced by loop

closure

A function that can access a variable in the scope of another function.

A closure is a function that accesses variables in the scope of another function

  1. Formation: nested functions within a function
  2. Function: calls external variables from inside the function, private properties of the constructor, and extends the life of the variable
  3. Advantages: You want a variable to be permanent in memory, modular code to avoid contamination of global variables, private attributes
  4. Disadvantages: Unable to reclaim referenced variables in closures, prone to memory leaks

Application of closures

  1. Successful callback to ajax request
  2. Callback method for event binding
  3. The delay callback of setTimeout
  4. Function returns another anonymous function inside the function

The following application scenarios:

  1. Constructor’s private property
  2. Calculate the cache
  3. Function throttling, anti – shake

Prototype and prototype chain

The prototype

Definition: An object that provides shared properties to other objects. Prototype for short.

All objects have an implicit reference, which is called the object’s prototype. When accessing an attribute of an object, it first looks for the attribute of the object itself, and if it fails to find it, it looks for its prototype object.

Prototype chain

Definition: If you want to access a property that doesn’t exist in an object, look for the object associated with prototype inside the object. When an attribute is found, it is layered through, and the association actually defines a chain of archetypes.

The prototype chain will eventually point to Object.prototype, and the prototype chain will end at Object.prototype.__proto__ (null).