Я пытаюсь понять разницу между доступом к переменной через prototype
и доступом к ней через this
. В одном объекте создается отдельная копия x
для this.x
и одна для *.prototype.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>
Почему печать 655, 855 в o1
, тогда как 855, 855 в o2
?