jQuery Setting global var
<p>I am using the following JS code in multiple places;</p>

<pre><code>$(this).attr("name")
</code></pre>

<p>I use it in diff places as;</p>

<pre><code>var currentKey = $(this).attr("name");
currentKeyVal = retrievedUserDataObj[$(this).attr("name")];
currentKeyVal = UserDataObj[$(this).attr("name")];
</code></pre>

<p>Now my question is is it possible to somehow make it as global variable so that the above code does not repeat ?</p>

<p>I am not sure if it can be made gloabl because of $(this) ?</p>

<p><strong>EDIT</strong>
Actual/Optimized code;</p>

<pre><code>    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
    })
}
</code></pre>
