Archive for the ‘ javascript ’ Category

Code: Pequeno exemplo de Prototipação no Javascript

Um pequeno exemplo de herança por meio de prototipação no Javascript, 
note que utilizei o metodo __proto__ e não prototype.

Vulgarmente: "javascript orientado a objetos"

<html>
<head>
<script>
var Ponto=function(){
	this.x=0
	this.y=0
}
var Circulo=function(){
	this.raio=10
	this.__proto__ = new Ponto()
}
var Imagem=function(){
	this.imagem="logo.png";
	this.init = function(){
		this.obj = document.createElement("img");
		this.obj.src=this.imagem;
		document.body.appendChild(new Object())
	}
	this.__proto__ = new Circulo()
}
function testar(){
	var obj = new Imagem();
	var circ = new Circulo();
	circ.x=10;
	alert('Image.imagem:'+obj.imagem+'\n Imagem.Circulo.Ponto.x:'+obj.x);
	alert('Circulo.raio:'+circ.raio+'\n Circulo.Ponto.x:'+circ.x);
}
</script>
</head>
<body onload="testar()">
</body>
</html>