" " :
function SomeClass() {
this.prop = 42;
document.body.addEventListener('click', (function() {
alert(this.prop);
}).bind(this));
}
new SomeClass();
, , , , , , ( , ., )
, , :
function SomeClass() {
var _this = this;
this.prop = 42;
document.body.addEventListener('click', function() {
alert(_this.prop);
});
}
new SomeClass();
- , , ,
function SomeClass() {
var prop = 42;
document.body.addEventListener('click', function() {
alert(prop);
});
}
new SomeClass();
, , , , "" , , , ( ) ) / , ( , )
EDIT: wait!, this.prop = 42,
, PoV , getter:
function SomeClass() {
var prop = 42;
document.body.addEventListener('click', function() {
alert(prop);
});
this.getProp = function() {
return prop;
};
this.setProp = function(newValue) {
prop = newValue;
};
}
new SomeClass();
, "this", , - :
function SomeClass(receiver) {
var prop = 42;
receiver.addEventListener('click', function() {
alert(prop);
});
}
new SomeClass(document.body);