Opening Lightning Strike (Core Understanding) :

In JS, function is also a kind of object instance, that is, function has dual properties of function and instance

Note: The gist of this sentence runs through all the summaries that follow

The process by which an object instance is generated

  1. In C++, every instance of an object is constructed by a constructor in a declared class, that is, a constructor in a class creates an instance
  2. In JS, similarly, a function creates an instance, except that the function does not appear in the class, that is, there is no need to declare a class at all, the properties of the class can be added dynamically

Add attributes dynamically for in-depth understanding

  1. First of all, the method is also a kind of attribute, so dynamic add attribute naturally can also add method!
  2. In instances where static properties are added, dynamic properties (methods) are added to the stereotype of the instance

The prototype

var fn= new Fun();
Copy the code

This is a statement that creates a function Object. The instance’s __proto__ and constructor’s prototype values are equal, both pointing to an empty Object. Start nesting doll, Object empty Object? Oh! It must be constructed by one of the constructors! And there must also be a __proto__ that points to an empty Object, but this grandfather is not called Object empty Object, it is Object prototype Object, the two are different, i.e

var object = new Object();
console.log(object.__proto__ instanceof Object); //false
Copy the code
  • Function’s “__ proto__” property

Functions have both function and instance properties

  1. All Function object instances are constructed by the Function() constructor, even Function() itself

  2. Object Prototype objects are not instances of Object

var object = new Object();
console.log(object.__proto__ instanceof Object); //false
Copy the code
var object = new Object();
console.log(Object.prototype instanceof Object); //false
Copy the code

Learn more about Instanceof through the prototype chain