Home > Web Front-end > JS Tutorial > body text

Why Does My JavaScript Code Fail When Using `setTimeout` With Function Expressions?

Linda Hamilton
Release: 2024-10-24 18:50:41
Original
889 people have browsed it

Why Does My JavaScript Code Fail When Using `setTimeout` With Function Expressions?

JavaScript Function Declaration and Evaluation Order

Why is the First Example Failing?

In JavaScript, code execution involves two phases: compilation and evaluation. The first example fails because of a fundamental misunderstanding between function declarations and expressions regarding these phases.

Function Declarations vs. Expressions

Function declarations use the function keyword and follow the syntax:

function name (arguments) {code}
Copy after login

Function expressions, on the other hand, are written in expression context and follow the same syntax as declarations, except that they are wrapped in parentheses. Expressions are processed during execution, not compilation.

Case Analysis

Example 1:

(function() {
setTimeout(someFunction1, 10);
var someFunction1 = function() { alert('here1'); };
})();
Copy after login

Compilation: SomeFunction1 is defined as undefined.

Execution: setTimeout is called with the undefined value of someFunction1.

Example 2:

(function() {
setTimeout(someFunction2, 10);
function someFunction2() { alert('here2'); }
})();
Copy after login

Compilation: SomeFunction2 is declared as a function..

Execution: setTimeout is called with the compiled someFunction2 function.

Example 3:

(function() {
setTimeout(function() { someFunction3(); }, 10);
var someFunction3 = function() { alert('here3'); };
})();
Copy after login

Compilation: SomeFunction3 is initially defined as undefined.

Execution: An anonymous function is passed to setTimeout, creating a closure to someFunction3. Later, someFunction3 is assigned a function, which overrides its undefined value.

Example 4:

(function() {
setTimeout(function() { someFunction4(); }, 10);
function someFunction4() { alert('here4'); }
})();
Copy after login

Similar to Example 2, someFunction4 is declared before being passed to setTimeout.

Summary

The first example fails because someFunction1 is not declared before being passed to setTimeout during compilation. Function expressions must be evaluated during execution, after declarations have been processed. Therefore, the order of expressions is crucial when using function expressions, especially when passed to asynchronous functions like setTimeout.

The above is the detailed content of Why Does My JavaScript Code Fail When Using `setTimeout` With Function Expressions?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!