prototype

Wenn ein Objekt aus einem Konstruktor erzeugt wird z.B. var Jetzt = new Date();, so verweist die Referenz-/Instanzvariable nicht direkt auf ihn, sondern auf dessen Prototype-Objekt.



<script>
function A(){ this.x = 5; }
function B(){ this.y = 6; }

B.prototype = new A();   Hier wird die Prototype-Eigenschaft von B überschrieben, und zwar mit einem Objekt des Konstruktors A.

var b = new B();

alert(b.x); // => 5
alert(b.y); // => 6

</script>


Quelle: Quelle: http://www.documentroot.net/allgemein/js-tutorial-klassen-prototypen-oop




/* bad way */

Car = function(){
this.doors = 4;
this.getDoors = function(){ // privileged method
return this.doors;
}
}

myCar = new Car();
alert(myCar.getDoors()); // 4


/* better way */

Car = function(){

}

Car.prototype.doors = 4;

Car.prototype.getDoors = function(){ // public method
return this.doors;
}

myCar = new Car();
alert(myCar.getDoors()); // 4


Quelle: http://www.brain-dump.org/docs/oop-in-js/prototype.html