Ich versuche zu verstehen, den Unterschied zwischen dem Zugriff auf eine Variable über prototype
vs Zugriff auf die gleiche über this
. In einem Objekt scheint es eine separate Kopie von x
eins für this.x
und eins für *.prototype.x
zu schaffen. Aber in anderen sieht beides auf dieselbe Kopie aus.Würde der Zugriff auf eine Prototypvariable 'x' über this.x eine separate Kopie erstellen?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Testing Promises</title>
</head>
<body>
<button type="button" onclick="test()"> Click Me</button>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
function Estimate() {
}
Estimate.prototype.sharedVar = 888;
Estimate.prototype.print = function() {
console.log("****************************************************");
console.log("* Shared Variable : ", this.sharedVar);
console.log("* Estimate Shared Variable : ", Estimate.prototype.sharedVar);
console.log("****************************************************");
}
Estimate.prototype.update = function (value) {
this.sharedVar = value + 100;
Estimate.prototype.sharedVar = value + 300;
}
function test() {
var o1 = new Estimate();
var o2 = new Estimate();
o1.print();
o2.print();
o1.update(555);
o1.print();
o2.print();
}
</script>
</body>
</html>
Warum 655, 855 in o1
während 855, 855 in o2
druckt?