私はprototype
経由で変数にアクセスすることと、同じものにアクセスすることの違いをthis
で理解しようとしています。 1つのオブジェクトでは、this.x
と*.prototype.x
の1つの別個のコピーを作成するように思われます(x
)。しかし、他の点では、両方の点が同じコピーに見えるように見えます。このプロトタイプ変数 'x'にthis.xでアクセスすると、別のコピーが作成されますか?
<!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>
なぜそれがo2
で855に対しo1
655、855、855を印刷しない ?あなたは、あなたが簡単にX
に自分自身をプロパティを追加することができ、これらの要素間の共有値をしたい場合はX
のすべてのインスタンスに(ここではY
)同じプロパティを追加するか、グローバル変数を作成することができたときに