series

Front – Type judgment

Front end octuwen – Inheritance

Front – Write new, bind, call and apply by hand

Words written before a serial

Graduated to work, do front-end also have so many years, as a non-class background, and by self-study halfway, has been exploring the front-end how to advance the problem, personally feel, eventually or return to the foundation. I don’t know if you are the same as me, I learned a lot of things, and finally forget again after a period of time, so I decided to write a set of basic articles, in order to forget, when needed can quickly recall. It might even help people who are just starting out. Encourage with everyone. Feel free to point out any mistakes in the comments section.

Prototype: __proto__

  • Prototype: function properties (each function has an object property called a prototype object)
  • Constractor: a reference to the function that created this object (constractor in Prototype refers to this constructor)
  • __proto__ : Attributes that every object (including functions) has
Note: in most cases __proto__ can be interpreted as "constructor prototype", i.e. : __proto__ === constract.prototype (objects created through object.create () do not apply to this equation)Copy the code
Var a = {}; console.log(a.prototype); // undefined console.log(a.constructor === Object); // Object console.log(a.__proto__ === Object.prototype); // true console.log(a.__proto__ === a.constructor.prototype); */ var Foo = function(){}; var foo = new Foo(); console.log(foo.constructor === Foo); / / true instance constructor to its constructor console. The log (foo constructor = = = foo prototype. The constructor); // True the constructor points to the console.log(foo.__proto__ === foo.prototype); / / true instance __proto__ is equal to the constructor of the prototype of the console, log (foo __proto__ = = = foo constructor. The prototype); Create () */ var a1 = {a:1} var a2 = object.create (a1); console.log(a2.__proto__); //Object {a: 1} console.log(a.__proto__ === a.constructor.prototype); //falseCopy the code

Second, prototype chain

Since __proto__ is a property of any object, and everything in JS is an object, a chain of __proto__ is formed, recursively accessing __proto__ and eventually null

var A = function(){}; var b = new A(); console.log(b.__proto__ === A.prototype); // console.log(b.__proto__.__proto__ === object.prototype); Console. log(b.__proto__.__proto__.__proto__); //nullCopy the code

Function and Object

Solve why:

Function instanceof Object; //true

Object instanceof Function; //true

The constructor of all constructors points to Function

Function. Constructor === Function; Constructor === Function; // Constructor === Function; The __proto__ constructor is a special anonymous Function Function () {} 4. This particular anonymous function's __proto__ points to the Object prototype. Function.__proto__ === Object. Prototype //true 5. __proto__ === function.prototype; // true Function.prototype === Function.__proto__; // trueCopy the code

Answer:

Function instanceof Object; Function.__proto__ => special anonymous functions special anonymous functionsCopy the code
Object instanceof Function;
Object.constructor = Function;
Object.__proto__ => Function.prototype
Copy the code