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
Release: 2016-08-27 08:53:29
Original
1063 people have browsed it
<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>
Copy after login

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>
Copy after login

Thank you everyone..

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template