The principle prototype of relevant knowledge -new

let obj = {}
let fn = function(){
  this.content = 'zhangsan'
} let fn2
= new fn()

fn2 an object is instantiated fn out, to learn new things to do, look at what things a normal object.

Ordinary objects:

Examples of objects fn2:

1: __proto__ a general object points and the fn2 Function.prototype __proto__ pointing fn.prototype, so the new process changes the point of the fn2 __proto__ to point to fn of the prototype.

2: fn2 the property has a content of fn, fn explanation of this point to fn2, so the new experience of the process of changing this point and call the fn1 fn method to add content property. fn.call (fn1).

The complete process:

Fn = the let function () {
  the this .content = 'zhangsan' 
} 
the let Fn2 = new new Fn () 
procedure: 
the let Fn2 = {}   // Create an empty object 
Fn2 .__ = fn.prototype proto__   // change the __proto_ fn2 _ pointing 
fn.call (fn2)   // change this point of fn

Implement its own new:

let myNew = function(sup){
  let obj = {}
  obj.__proto__ = sup.prototype
  sup.call(obj)
  return obj        
}
let fn = function(){
    this.content = 'zhangsan'
}
let fn2 = myNew(fn)
console.log(fn2)

 

 

 

Guess you like

Origin www.cnblogs.com/bravefuture/p/11278205.html