在本地重新分配全局变量,而不在全局范围内更改它们。JavaScript
我知道如何使代码按预期工作,但我尝试使用相同的变量名,并遇到了以下问题: 是否可以在提交设置中重新分配最小和最大变量,以便它们在全局范围内保持不变? 您可以在代码中看到为什么我希望它们在全局范围内保持不变 如果是,请告诉我如何以及在哪里可以了解有关此主题的更多信息在本地重新分配全局变量,而不在全局范围内更改它们。JavaScript,javascript,scope,global-variables,Javascript,Scope,Global Variables,我知道如何使代码按预期工作,但我尝试使用相同的变量名,并遇到了以下问题: 是否可以在提交设置中重新分配最小和最大变量,以便它们在全局范围内保持不变? 您可以在代码中看到为什么我希望它们在全局范围内保持不变 如果是,请告诉我如何以及在哪里可以了解有关此主题的更多信息 // here i get the HTML elements from <input>. let min = document.getElementById('min') let max = document
// here i get the HTML elements from <input>.
let min = document.getElementById('min')
let max = document.getElementById('max')
function submitSettings() {
// here i want to validate the values from those HTML elements while maintaining the same variable names (min, max).
function validateSettings() {
min = min.valueAsNumber // My question: How can i reassign global variables locally without changing them in global scope?
let max = max.valueAsNumber // Or: How can i get max variable from the scope from outside (from global scope)?
min >= max ? console.log("Not good") : console.log("OK")
}
validateSettings()
// here i want to clear <input>, but i can't because min and max are not HTML elements anymore, rather numbers.
min.value = ''
max.value = ''
}
由于代码突出显示的原因,隐藏变量通常被认为是不好的做法。很难将范围从一个保持到下一个。有不同的方法可以解决这个问题,但它们主要依赖于为本地上下文重命名变量
// here i get the HTML elements from <input>.
const min = document.getElementById('min')
const max = document.getElementById('max')
function submitSettings() {
// here i want to validate the values from those HTML elements while maintaining the same variable names (min, max).
function validateSettings() {
let minValue = min.valueAsNumber // My question: How can i reassign global variables locally without changing them in global scope?
let maxValue = max.valueAsNumber // Or: How can i get max variable from the scope from outside (from global scope)?
minValue >= maxValue ? console.log("Not good") : console.log("OK")
}
validateSettings()
min.value = ''
max.value = ''
}
但我认为这不是很好的做法
您还可以利用validateSettings是一个函数这一事实,为它设置最小和最大参数
// here i get the HTML elements from <input>.
const min = document.getElementById('min')
const max = document.getElementById('max')
function submitSettings() {
// here i want to validate the values from those HTML elements while maintaining the same variable names (min, max).
function validateSettings(min, max) {
min >= max ? console.log("Not good") : console.log("OK")
}
validateSettings(min.value, max.value)
min.value = ''
max.value = ''
}
由于存在不同的作用域,因此可以再次使用let min和let max。不确定这是否是你想要的…我知道我可以使用不同的变量。这更像是一个教育问题。我想了解范围,这。。。在我遇到的示例中,如果不使用阴影,那么对于同一个idea/变量,可能会有很多不同的名称,例如:min,myMin,minim…?可能会有一些名称,但通常可以在文件之间分割代码,并避免大部分名称。如果这是一个更大的问题,您可以按照第二个示例,使用let关键字重新定义同一变量的本地版本。但是要知道,您不能再次访问该范围内的全局变量。我又添加了一个示例,说明如何利用validateSettings是一个函数这一事实来完成您想要的任务。这很简单:谢谢
// here i get the HTML elements from <input>.
const min = document.getElementById('min')
const max = document.getElementById('max')
function submitSettings() {
// here i want to validate the values from those HTML elements while maintaining the same variable names (min, max).
function validateSettings(min, max) {
min >= max ? console.log("Not good") : console.log("OK")
}
validateSettings(min.value, max.value)
min.value = ''
max.value = ''
}