The solution is as follows: in the outer function,
write this
to any variable and this variable
will be available in the inner function, like all
variables (usually this variable is called self
).
Thus, we will pass this
from the outer function
to the inner one:
"use strict";
let elem = document.querySelector('#elem');
elem.addEventListener('blur', parent);
function parent() {
console.log(this.value); // shows 'text'
let self = this; // write this to any variable, for example, to self
function child() {
console.log(self.value); // shows 'text'
}
child();
}
Let this code be given:
<input id="elem" value="3">
"use strict";
let elem = document.querySelector('#elem');
elem.addEventListener('blur', func);
function func() {
alert( square() );
function square() {
return this.value * this.value;
}
}
The author of the code wanted the square of the
number from input value
to be displayed
on the screen when focus was lost. However, for
some reason, when focus is lost, an error is
generated in the console. Please fix the code
author's mistake. Write a text in which you will
explain to the author of the code why this
error occurred.