Home > Computer Tutorials > Computer Knowledge > Use JavaScript to assign a value to a variable based on the value of a drop-down menu

Use JavaScript to assign a value to a variable based on the value of a drop-down menu

WBOY
Release: 2024-01-25 08:27:20
forward
653 people have browsed it

Use JavaScript to assign a value to a variable based on the value of a drop-down menu

javascript determines the value of the drop-down menu and assigns it to the variable

Personal testing is available.

You need to put the definition of variable i in the initialization function, rather than defining it as a global variable.

What is the difference between assigning anonymous functions to variables in JavaScript and directly naming functions

Originally I didn’t like to answer such a general question. But I can't bear to see other wrong answers misleading people, so I'll just answer it briefly.

The biggest difference between the two writing methods is:

var init = function () { }; is a function expression. The function is only executed when the code execution reaches the current line, and init is assigned a value.

function init() {} is the declaration of a function. Like var, it will be defined at the front of the code.

So, the following two examples:

foo(); // Calling foo after the function declaration can be called normally. Because foo is defined to the front.

function foo() {

return true;

}

foo(); // Call the function before the function expression and report an error. Because there is no foo variable at this time.

var foo = function() {

return foo;

};ES5 stipulates that functions can only be declared in the top-level scope and function scope, otherwise it is illegal. For example:

if( true ) {

// It is wrong to define functions outside the top-level and function scope. Although the browser may not necessarily throw an error, it is not standardized.

function foo() {

return true;

}

}

ES6 introduced the concept of block-level scope, and this definition method is allowed. A function declared in a block-level scope has a scope similar to a variable declared using let and cannot be called outside the block-level scope.

For example:

{

function foo() {

return true;

}

}

foo(); // Calling a function outside the code block where the function is declared will throw an error.

The above is the detailed content of Use JavaScript to assign a value to a variable based on the value of a drop-down menu. For more information, please follow other related articles on the PHP Chinese website!

source:docexcel.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template