[Object-oriented] ---- [prototype && __ proto __ && relationship between the three instances of object] (d) (reprinted teacher Alley- alley self)

1, the constructor

Copy the code
a, what is the constructor? 
     Explanation: The function creates a new keyword called a constructor 
     function: to create an object 

without further ado directly on the code, we first create a constructor humans

 

Then we create two instances, a mundane a Daniel! After all, only two individuals we can match the value of the Yen
 
  
 

 

 

 We can see the mundane Kazuhiko ancestral property has a name and a way to eat; we simply think about as mundane Kazuhiko ancestors were created out of the constructor new Person, then the two of them eat the methods are equal?

 

Print result is false. Why is false? This time we have to say a classic face questions up! ! !
 
When creating constructors js which actions?
1, opened up a space in the memory
2, put this point to the current object
 
Through this interview questions you can imagine why not equal, and point to address is not the same and you still expect them to do the same? If you do not understand we can also give a user-friendly chestnuts, suppose you have a child, and next door to Pharaoh, that you have a common approach "to educate their children," You can imagine a next door to you and Pharaoh's children are not in together, the two educational methods, although the same name, but you can count on Pharaoh educated children and your children can be educated in exactly the same? Do not naive child, if the same is the case then this world would not have the Zhongxiao FELL Lim
 
So the question is how can we make them two methods are equal it? That is how to make them all call the same method, after all, each time calling different methods What a waste of memory! ! ! !
 
 
 
Can we then we will write out the object of this method to make this method of execution address they call out this function?

 

 

貌似可以!!!但是如果要是有多个方法的时候该怎么办?难到我需要在外面定义多个全局函数吗?毕竟我们主张的是尽量减小全局变量和全局函数,第一是为了防止变量名的冲突。第二也是为了初学者的词汇量着想 第三也是为了防止变量的污染
 
难到就没有什么完美的解决方案吗?
那下面就不得不说下我们伟大的原型prototype
Copy the code

 

2、原型prototype

Copy the code
1、什么是prototype?
     a、prototype是每一个函数自带的一个属性
     b、prototype属性指向一个对象,简称原型。所有prototype称为原型

2、原型有什么作用?
     1、节约内存

     2、扩展属性和方法

     3、可以实现类的继承

3、接下来我们创建2个函数,来查看函数里面是否有prototype这个属性

从以上例子中我们可以更加肯定每一个函数都有一个prototype属性而这个属性指向一个object。如果prototype指向一个对象的话那么我们自然就可以给它添加属性和方法了

 

4、如何在prototype原型上面添加属性和方法?

 

 

5、因为这个方法是在构造函数Person的原型上添加的,因此当每次实例化一个对象的时候,每个对象都有这样一个方法,而且都是调用的同一个方法

 

 6、那么接下来我们看下我们加在原型上面的这个方法在这个实例化对象的哪里放着。

我们可以清晰的看到我们实例化出来的对象,这个eat方法在__proto__里面放着,而这个__proto__同样也指向了一个对象。那么我们就不得不思考下构造函数的prototype与实例化对象里面的__proto__是否是同一个东西

 

7、构造函数的prototype与实例化__proto__之间的关系

不比较不知道,一比较吓一跳。他们不仅发现里面的东西相同,而且判断结果也相同。而且你会发现你通过原型prototype添加的方法在__proto__里面也会出现。换句话说也就是__proto__可以访问prototype里面的所有属性和方法

Copy the code

 3、prototype--->__proto__---->实例化对象三者之间的关系

Copy the code
1、prototype总结

     解释:每一个函数都有一个prototype这个属性,而这个属性指向一个对象,我们简称原型
     
     作用:
          1、节约内存

          2、扩展属性和方法

          3、可以实现类之间的继承

2、__proto__总结
     1、每一个对象都有一个__proto__属性

     2、 __proto__指向创建自己的那个构造函数的原型

     3、对象可以直接访问自己__proto__里面的属性和方法

3、constructor总结
     constructor指向创建自己的那个构造函数


接下来我们说说三者之间的关系。

当我们创建一个构造函数的时候这个构造函数自带了一个prototype属性,而这个属性指向一个对象,也就是原型对象。这个原型对象里面有一个constructor构造器,它的作用是指向创建自己的构造函数。除此之外prototype还可以存放公共的属性和方法。当我们实例化一个对象的时候,这个对象自带了一个__proto__属性,这个__proto__指向创建自己的构造函数的原型对象。可以使用这个原型对象里面的属性和方法。那么接下来我们用一个栗子和内存图来表示

 

 
Copy the code

 

Guess you like

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