gpt4 book ai didi

javascript - 重构不同作用域的变量

转载 作者:行者123 更新时间:2023-11-30 09:39:17 27 4
gpt4 key购买 nike

$(function () {
var wrapper = $('.table-wrapper');
var table = $('.multiples-table');
var filter = $('.filter');

//use these variables in calculations

$('#filter').keyup(function () {
var wrapper = $('.table-wrapper');
var table = $('.multiples-table');
var filter = $('.filter');

//use the same variables in different calculations
});
});

如何重构这些变量,以便我只需声明一次?谢谢。

最佳答案

因此,这些变量已经特定于最顶层函数的内容。因此,除非您在该函数的某处更改它们,否则您可以直接在 keyup 函数中直接使用它们:

$(function () {
var wrapper = $('.table-wrapper');
var table = $('.multiples-table');
var filter = $('.filter');

//use these variables in calculations

$('#filter').keyup(function () {
// just use wrapper, table and filter here, as is. No need to re-declare them

//use the same variables in different calculations
});
});

希望这对您有所帮助!

关于javascript - 重构不同作用域的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41989096/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com