Home >Backend Development >PHP Tutorial >JS: How can a new object inherit Array from prototype?

JS: How can a new object inherit Array from prototype?

WBOY
WBOYOriginal
2016-08-27 08:53:291062browse
<code><script type="text/javascript"> 

function Person(){} 

Person.prototype.array = new Array("Koji", "Luo"); 

Person.prototype.showArray = function(){ 
alert(this.array); 
} 

var obj1 = new Person(); //生成一个Person对象 
var obj2 = new Person(); 

obj1.array.push("Kyo"); //向obj1的array属性添加一个元素 

obj1.showArray(); //Koji,Luo,Kyo 
obj2.showArray(); //Koji,Luo,Kyo 

</script> </code>

If an element is added to the array of obj1, it will also be added to the array of obj2.
Is the array they inherit a pointer? Everyone has the array in the prototype object?

<code>//最后问一个很奇怪的问题(prototype中的array难道也只是一个指针?)
</code>

Thank you everyone..

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn