Answer the question
In order to leave comments, you need to log in
What is the correct way to access an outer object from an inner one in JavaScript?
There is the following code:
function mock() {
this.val = 'val';
this.log = {
start: function() {
this.timer = setInterval(function() {
console.log(this.val);
}.bind(this), 1000);
}.bind(this),
stop: function() {
clearInterval(this.timer);
delete this.timer;
}.bind(this)
};
}
Answer the question
In order to leave comments, you need to log in
In your case, this refers to the root object everywhere, so nothing prevents you from getting the nested one and working with its properties:
function Mock() {
this.val = 'val';
this.log = {
start: function() {
this.log.timer = setInterval(function() {
console.log(this.val);
}.bind(this), 1000);
}.bind(this),
stop: function() {
clearInterval(this.log.timer);
delete this.log.timer;
}.bind(this)
};
}
function Timer(handler, interval) {
this.handler = handler;
this.interval = interval;
}
Timer.prototype.start = function () {
this.timer = setInterval(this.handler, this.interval);
};
Timer.prototype.stop = function () {
clearInterval(this.timer);
delete this.timer;
};
function Mock() {
this.val = 'val';
this.log = new Timer(function () {
console.log(this.val);
}.bind(this), 1000);
}
if I understand correctly, then you want to get the value of the object by calling this.val anywhere in the code, but this is not correct.
Your case is the following:
let m = new mock();
m.log.start();
m.log.stop();
function mock(number) {
this.number = number;
this.val = 'val';
this.log = {
start: function() {
this.timer = setInterval(function() {
console.log(this.number, ": ", this);
}.bind(this), 1000);
}.bind(this),
stop: function() {
clearInterval(this.timer);
delete this.timer;
}.bind(this)
};
}
m = new mock('1')
> mock {number: "1", val: "val", log: Object}
n = new mock('2')
>mock {number: "2", val: "val", log: Object}
m.log.start(); n.log.start()
VM2668:7 1 : mock {number: "1", val: "val", log: Object, timer: 6480}
VM2668:7 2 : mock {number: "2", val: "val", log: Object, timer: 6481}
VM2668:7 1 : mock {number: "1", val: "val", log: Object, timer: 6480}
VM2668:7 2 : mock {number: "2", val: "val", log: Object, timer: 6481}
VM2668:7 1 : mock {number: "1", val: "val", log: Object, timer: 6480}
VM2668:7 2 : mock {number: "2", val: "val", log: Object, timer: 6481}
VM2668:7 1 : mock {number: "1", val: "val", log: Object, timer: 6480}
VM2668:7 2 : mock {number: "2", val: "val", log: Object, timer: 6481}
VM2668:7 1 : mock {number: "1", val: "val", log: Object, timer: 6480}
VM2668:7 2 : mock {number: "2", val: "val", log: Object, timer: 6481}
VM2668:7 1 : mock {number: "1", val: "val", log: Object, timer: 6480}
VM2668:7 2 : mock {number: "2", val: "val", log: Object, timer: 6481}
VM2668:7 1 : mock {number: "1", val: "val", log: Object, timer: 6480}
VM2668:7 2 : mock {number: "2", val: "val", log: Object, timer: 6481}
VM2668:7 1 : mock {number: "1", val: "val", log: Object, timer: 6480}
VM2668:7 2 : mock {number: "2", val: "val", log: Object, timer: 6481}
VM2668:7 1 : mock {number: "1", val: "val", log: Object, timer: 6480}
VM2668:7 2 : mock {number: "2", val: "val", log: Object, timer: 6481}
m.log.stop(); n.log.stop()
What you are looking for is similar to arrow functions in ES6.
I would do like this:
function mock() {
this.val = 'val';
this.startlog = () => {
this.timer = setInterval(() => {
console.log(this.val);
}, 1000)
};
this.stopLog = () => {
clearInterval(this.timer);
delete this.timer;
}
}
class Mock {
constructor() {
this.val = 'val';
}
startlog() {
this.timer = setInterval(() => {
console.log(this.val);
}, 1000)
}
stopLog() {
clearInterval(this.timer);
delete this.timer;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question