This refers to the problem;
In ES5 this always refers to the object that last called it;
var name = "window";
var a = {
name : null.fn : function () {
console.log(this.name); }}var f = a.fn;
f(); // console.log(window);
Copy the code
Arrow function pointing in ES6;
- Arrow functions have no this object and refer to the outer function or scope of the object.
- The arrow function itself has no this object and looks for this bound objects along the scope chain; Find this object based on the scope chain;
- The arrow function itself does not have this, so there is no call,apply, or bind to change this;
const name = 'window';
function out() {
setTimeout((a)= > {console.log(this.name)});
}
const obj = {
name: 'hhhh'
};
out.apply(obj);
Copy the code
Anonymous function pointing;
Anonymous functions point to Windows and, in strict mode, to undfind;
function out() {
setTimeout(function() {
console.log(this) // This points to window, non-strict mode;
},50)}Copy the code
This object
Function calls allow functions to be reused from different contexts. Each function is called with two special variables: this,arguments;
Scope chain & closure
- Each function execution has a context, which holds information such as variables saved by the function execution.
- The scope chain wraps the orderly acquisition of access and changes to the execution environment.
- VO storage of variable objects: parameter/variable/function declarations;
The constructor
- Instantiation constructors create objects
- The constructor’s this points to the instantiation constructor itself
Learning of higher-order components;
There are functions as parameter inputs or functions as output outputs;Copy the code
Function as input
const arr = [1.2.3];
arr.map((item) = > item +1);
Copy the code
Function as output
const isType = (type) = > (obj) => {
return Object.prototype.toString.call(obj) === '[Object ' + type + '] ';
}
isType('String') ('111'); // true;
Copy the code