Answer the question
In order to leave comments, you need to log in
How does a function access itself when called recursively?
Is it correct to say that when a function is called recursively in code, the js engine looks for the declaration of the called function in the outer scope? Below is the code with comments.
function func() { //функция func объявлена в глобальном контексте
func();
/*в текущей области видимости 'func' не объявлена, поэтому движок
ищет объявление во внешней области видимости (в данном случае это
глобальная область видимости), находит его там и теперь может выполнить функцию*/
}
Answer the question
In order to leave comments, you need to log in
Everything is correct https://jsfiddle.net/yy38x0mg/
This logic is valid for any calls.
In js, if something is not declared, it always looks in the outer scope.
You can read more here: https://learn.javascript.ru/closures
'func' is not declared in the current scope
var f = function func() {
console.log(func.name); // 'func'
};
console.log(f.name); // 'func'
console.log(func.name); // Reference error так как func нет в этой области видимости
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question