FAQ / Knowledge record (1)

一 call apply bind

The same point:
you can change the this inside the function to point to the
difference:

  1. call and apply will call the function and change this in the function
  2. The parameters passed by call and apply are not the same. The parameters passed by call are arg1, arg2 ... The form apply is an array
  3. bind does not call functions, you can change this to pass by value like call

Main application scenarios:

  1. call often do inheritance
  2. Apply is often related to arrays, such as the maximum and minimum values ​​of arrays with the help of mathematical objects
  3. bind does not call the function but will change the this point, such as changing the this point of the timer

Which performance is better, call or apply?
Two parameters passed <=3a similar performance when >3the performance of call when relatively better than the performance of some apply
generally developed using call a little late

call-the previous parameter value The parameters behind the scope object can be multiple
apply-the first parameter is the scope object The second parameter must be an array
bind-bind a scope but not immediately

var b = a.fn
var c = b.bind(a,1,2)
c() // c的作用域是a

2. What is a higher order function

1. Function as parameter
2. Function as return value

What is the difference between the three-arrow function and the ordinary function (function)?

  1. Arrow function syntax is more concise than ordinary functions
  2. The arrow function does not have its own this. Any method such as this call and apply in the context to which this inherited function belongs cannot change this.
  3. There are no arguments (array-like) in the arrow function, only ... arg gets the passed parameter set
  4. The arrow function cannot be executed by new because the arrow function does not have its own this and prototype

Four points of different calling methods of this function point to

  1. Ordinary function call
fn();// this指向window
  1. Method call
obj.fn(); // this指向obj
  1. Call this inside the constructor as a constructor to point to the object created by the constructor
  2. As a handler at the event
btn.onClick=function(){}; // 触发该事件的对象
  1. As a parameter of the timer
setInterval(function(){},1000) // this指向window

Summary: this inside the function is not determined when writing, it is determined by the function when it is called

Change this to
this pointer, the current object (scope object) is the window without the caller

  1. Arrow function
  2. Declare a variable to save the pointer var that = this
  3. call executes a function function name. call (xx) places the function in a specific scope object for execution
  4. apply function name.apply (scope object)

Five strict mode

  1. Eliminate some unreasonable and irrational JS syntax and less weird behavior
  2. Eliminate some unsafe aspects of code operation and ensure the safety of code operation
  3. Improve compiler efficiency and increase running speed
  4. Disabled some grammars that may be defined in future versions of ECMAScript to pave the way for future new versions of Javascript. Such as reserved words such as class export extends import super. Cannot do variable names.
    Turn on strict mode
'use strict' // 下面就会安装严格模式执行代码

Variables in strict mode

  1. Must be declared before use
  2. Can't delete the declared variables at will

Strict mode this points to the problem

  1. This in global scope is undefined
  2. If the constructor does not add new, calling this points to undefined
  3. The timer this still points to the window

Function changes

  1. Duplicate function parameter names are not allowed
  2. Not allowed to declare functions in non-function code blocks
Published 41 original articles · Likes2 · Visits 1836

Guess you like

Origin blog.csdn.net/weixin_43883485/article/details/104703751