Hi 
Ich hab jetzt mal mit folgendem Script herumexperimentiert:
Scheint alles super zu funktionieren!!
Man kann scheinbar zwischen privaten und öffentlichen Eigenschaften/Methoden unterscheide.
Ist das jetzt die offizielle Vorgehensweise, wie man in JavaScript objektorientiert programmiert?
Ich hab jetzt mal mit folgendem Script herumexperimentiert:
HTML:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script language="JavaScript">
"use strict";
// Shape - superclass
function Shape() {
// private:
var z = 0;
function f()
{
return 2;
}
// public:
this.x = 0;
this.y = 0;
// superclass method
this.move = function(_x, _y, _z) {
this.x += _x + f();
this.y += _y + f();
z += _z + f();
console.info("Shape moved.");
};
this.getZ = function() {
return z;
};
}
// Rectangle - subclass
function Rectangle() {
Shape.call(this); // call super constructor.
// public:
this.u = 0;
this.move2 = function(_x, _y) {
this.u += 100;
this.x += _x;
this.y += _y;
console.info("Shape moved 2.");
};
}
// subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;
var rect0 = new Rectangle();
var rect1 = new Rectangle();
rect0 instanceof Rectangle // true
rect0 instanceof Shape // true
rect1.move(1, 1, 1); // Outputs, "Shape moved."
alert(rect0.x); // 0
alert(rect1.x); // 3
alert(rect0.getZ()); // 0
alert(rect1.getZ()); // 3
rect1.move2(8, 8); // Outputs, "Shape moved 2."
alert(rect0.x); // 0
alert(rect1.x); // 11
alert(rect1.u); // 100
alert(rect1.z); // undefined
alert(rect1.f()); // Error: is not a function
</script>
</head>
</html>
Scheint alles super zu funktionieren!!
Ist das jetzt die offizielle Vorgehensweise, wie man in JavaScript objektorientiert programmiert?