【问题标题】:jQuery Setting global var [duplicate]jQuery设置全局变量[重复]
【发布时间】:2023-03-04 22:50:01
【问题描述】:

我在多处使用下面的JS代码;

$(this).attr("name")

我在不同的地方使用它作为;

var currentKey = $(this).attr("name");
currentKeyVal = retrievedUserDataObj[$(this).attr("name")];
currentKeyVal = UserDataObj[$(this).attr("name")];

现在我的问题是,是否有可能以某种方式将其设为全局变量,以使上述代码不再重复?

我不确定它是否可以因为 $(this) 而成为 gloabl ?

编辑 实际/优化代码;

    function setFormFieldValues()
{
var currentKey,currentKeyVal;
    if (supports_html5_storage())
    {
    var retrievedUserDataObj = JSON.parse(localStorage.getItem('UserDataObj'));
    localStorageSupport = true;
    }

    $(".formFieldUserData").each(function(){
        var $this = $(this);
        currentKey = $this.attr("name");
        currentKeyVal = UserDataObj[currentKey]; //Set with default values initially

        if (localStorageSupport)
        {
            if(retrievedUserDataObj) //called when there are some values in localStorage
                currentKeyVal = retrievedUserDataObj[currentKey];
        }
        else
        {
            if ($this.val() != "")
                currentKeyVal = $this.val();
        }

        $("#"+currentKey).val(currentKeyVal); //Input text box
        $("#"+currentKey+"Txt").html(currentKeyVal); // Form label
    })
}

【问题讨论】:

  • 我认为你可以做到这一点window.foo = $(this)。但是 js 中的this 确实不一样。我认为您需要提供更多背景信息。
  • 鉴于this 的上下文会根据代码中的位置而变化,将其设为全局变量有什么意义?这只有在 this 在任何上下文中引用 very same 元素这一不太可能发生的情况下才有意义。

标签: javascript jquery


【解决方案1】:

只使用一个函数来进行处理可能更容易;我不确定为什么currentKeyVal 被定义了两次:

// define outside the function to make them global
var currentKey, currentKeyVal;

function getCurrentKeys(element){
    currentKey = $(element).attr("name");
    currentKeyVal = retrievedUserDataObj[currentKey];
    currentKeyVal = UserDataObj[currentKey];
}

如下使用:

getCurrentKeys(this);

更新添加 cmets 中描述的优化:

function setFormFieldValues()
{
    var currentKey,currentKeyVal;
    if (supports_html5_storage())
    {
    var retrievedUserDataObj = JSON.parse(localStorage.getItem('UserDataObj'));
    localStorageSupport = true;
    }

    $(".formFieldUserData").each(function(){
        var $this = $(this);
        currentKey = $this.attr("name");
        currentKeyVal = UserDataObj[currentKey]; //Set with default values initially

        if (localStorageSupport)
        {
            if(retrievedUserDataObj) //called when there are some values in localStorage
                currentKeyVal = retrievedUserDataObj[currentKey];
        }
        else
        {
            if ($this.val() != "")
                currentKeyVal = $this.val();
        }

        $("#"+currentKey).val(currentKeyVal); //Input text box
        $("#"+currentKey+"Txt").html(currentKeyVal); // Form label
    });
}

是的,您可以使用ternary operators 对代码进行更多优化,但这会使代码更难阅读。

【讨论】:

  • Thx...我已经用实际代码编辑了原始问题...所以您可以更好地理解我为什么按照我的编码方式完成...
  • 好吧,我看到你可以做两个优化:(1).each() 函数内的第一行可以是var $this = $(this),并使用它而不是重复定义这个 jQuery 对象。 (2) 多次使用currentKey 而不是$(this).attr("name");,就像我在上面的代码示例中所做的那样。
  • 能否请您在我原来的问题中编辑相同的内容,以便我可以更好地理解优化...此外,您还可以建议任何其他优化...
  • 或者我现在已经编辑了......你能检查一下代码现在看起来是否完全优化......
  • 是的,我就是这么做的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-28
  • 2014-06-22
  • 1970-01-01
相关资源
最近更新 更多