[Object-oriented] vernacular version (four) ------ [Piece]

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?

 

 

It looks like you can! ! ! If there are multiple methods but if the time how to do? I need to define more difficult global function in the outside? After all, we advocate is to minimize the global variables and global functions, the first is to prevent conflicts of variable names. The second is for the sake of the third vocabulary for beginners but also to prevent contamination variable
 
So difficult that there is nothing perfect solution?
那下面就不得不说下我们伟大的原型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 summary 

     explanation: Each function has a prototype of this property, and this property to an object, we referred prototype 
     
     effect: 
          1, save memory 

          2, extended attributes and methods 

          3, between the inherited class may implement 

2, __ proto_ _ summary 
     1, every object has a property __proto__ 

     2, __proto__ point to create a prototype of the constructor's own 

     3, the object can directly access their properties and methods inside __proto__ 

3, constructor summary 
     constructor points to create your own that the constructor 


then we talk about the relationship between the three. 

When we create a constructor when the constructor comes with a prototype property, and this attribute points to an object, which is the prototype object. The prototype object inside a constructor constructor, its role is to point to create your own constructor. In addition prototype can also store the public properties and methods. When we instantiate an object when the object comes with a __proto__ property, this __proto__ point to create your own prototype object constructor. This can be used inside the prototype object properties and methods. So then we use a graph to represent chestnuts and memory

 

 
Copy the code

Guess you like

Origin www.cnblogs.com/fanzenghui/p/11286718.html