[Object-oriented] twitched method that "magic" of the object-oriented inheritance (V) with a large vernacular (Reprinted self-teacher Alley- alley)

001 prototype chain

Copy the code
What is the prototype chain? 
     Explanation: __proto__ called a chain formed by the prototype chain 

     Suppose we create a Person constructor, then instantiate an object is fanchen, we output when the object is instantiated, the instance of the object inside a __proto__ property, this property __proto__ point is to create a prototype of the constructor's own, that is to say __proto__ instantiate an object of this points to a Person inside prototype prototype object because prototype is therefore an object inside certainly there will be a __proto__. And this __proto__ point is to create a Person object constructor, imagine who created the Person? This time we have to say, "Everything is an object." Is certainly an object to create a Person. And this object is the Function. There are objects so sure there will be __proto__. So we can imagine what you can create objects out of it? So the top of the object is null. Next we use case and memory map detailed explanation wave

       

       

 

 

to sum up:
     Prototype chain: the chain formed by __proto__
          Object inside __proto__ point is null
Copy the code

 002 Methods inherited

Copy the code
Inheritance: 
     In the last chapter we say that the property is inherited, then we went Methods inherited. Inherited method is divided into a good variety, then we analyzed one by one to find the most perfect way of inheritance 

     first, let's look at property inheritance, let's write a constructor Person. And then create an instance of the object fanchen

 

 

 现在我们可以继承到父级的属性了,但是貌似父级的方法继承不了,因为我看到__proto__里面只有一个work这个方法。那么我们如何继承父级的方法呢?我们可以试想一样fanchen是如何访问到work这个方法的?因为实例化对象里面的__proto__指向是创建自己的那个构造函数的原型对象因此可以访问。那么如果现在我用Man的原型对象指向Person的原型对象会发生什么?
Copy the code

 1、原型继承

Copy the code
Man的原型对象指向Person的原型对象
 

 

貌似是可以的,但是这个原理是什么呢?同时注意一下上面的constructor的指向。接下来我们画一个内存图来解释一下原理和弊端。

从上面的内存图中我们不难看出当Man的原型指向Person的原型的时候首先Man的原型对象断掉了,随后我们又给Man的原型添加了方法work,这个时候你会发现Man的work方法加到了Person身上。这样肯定是不行的,因为我们怎么能修改父级的原型对象呢?顺便我们可以在打印出来父级的原型看一下

 

因此这个原型继承肯定是不行的,那么接下来我们说下原型继承缺点
 
缺点:子类原型发生改变父类原型也会发生改变
Copy the code

 2、原型拷贝

Copy the code
上面的方法肯定不是特别合理的,那么我们可以想一下既然不能直接将Man的原型指向Person的原型,那么我们可不可将Person里面原型对象的属性直接拷贝一份,这样我就不会直接修改到父级的原型了?
废话少说直接上代码

  

貌似特别完美,接下来我们在查看一下父级有没有发生改变

真的没有什么问题啊。那么接下来我们通过内存图来查看下原理及缺点

 

 

从内存图中可以看出我们的Man只是拷贝了一份Person的方法,然后Man里面的方法的指向还是指向Person的里面方法的原型。随后我们又在Man上面添加了一个work方法。这样我们既不会污染Person也能实现了继承,但是缺点也显而易见了
 
缺点:假设Person上面还有父级  那么我们的Man是无法访问到Person父级的原型上的方法
Copy the code

 3, the prototype inheritance chain

Copy the code
From the previous chapter we talk about the prototype chain can know that we can pass some of the properties and methods of the prototype chain to access the parent and the parent's parent inside, so we take advantage of this feature Man prototype object can point to an instance of the Person, because examples I'm sure there is a __proto__, so we formed a prototype chain. Next we use case diagram to represent and memory

  

  

  When the print came out really can see the parent's method also can be accessed, but there are shortcomings, we can easily find Man of __proto__ inside more than a few age name sex and the value is undefined?

Copy the code

 4, mixed inheritance

Copy the code
As it can be seen from the above prototype chain to inherit, and when printed out fanchen We lost a constructor property but also a lot of messy property, then how do we solve it? We go step by step
  

  

This time you will find that finally achieved the perfect inheritance.

 
 
Copy the code

 5, parasitic inheritance

Copy the code
The Internet will often see a method called a parasitic inheritance. In fact, to see the name you can think of, then we need something similar to a parasite is the same. In fact, a function center
 

 

 

 

 
 
Copy the code

 

Guess you like

Origin www.cnblogs.com/mp-0518/p/11440520.html