Ответ 1
Сохраняйте текущий this
в какой-либо другой переменной в Chart
, как этот
function Chart() {
var self = this;
this.draw = function(data) {
data.forEach(function(value) {
//do something with values
console.log(self);
});
}
};
Кроме того, вы можете передать this
следующим образом: Array.prototype.forEach
принимает this
arr.forEach(callback[, thisArg])
Например,
this.draw = function(data) {
data.forEach(function(value) {
//do something with values
console.log(this);
}, this); // Pass the current object as the second parameter
}