Skip to main content

Replace this Qualifier With Local Variable (JavaScript)

In This Article

Replaces the suspicious use of the “this” qualifier in the inner function with a new local variable declared (and assigned) in the outer scope.

#Availability

From the context menus or via shortcuts:

  • when the caret is on the suspicious “this” qualifier in an inner function.

#Example

var Sum = new function () {
    var element1 = document.getElementById("sumElement1");
    var element2 = document.getElementById("sumElement2");
    ProcessClick();
    this.val1 = parseInt(element1.nodeValue);
    this.val2 = parseInt(element2.nodeValue);
    Proc = function () {
        return this.val1 + this.val2;
    }
    var resElement = document.getElementById("result");
    resElement.nodeValue = this.Proc();
};

Result:

var Sum = new function () {
    var element1 = document.getElementById("sumElement1");
    var element2 = document.getElementById("sumElement2");
    ProcessClick();
    this.val1 = parseInt(element1.nodeValue);
    this.val2 = parseInt(element2.nodeValue);
    var val1 = this.val1;
    var val2 = this.val2;
    Proc = function () {
        return val1 + val2;
    }
    var resElement = document.getElementById("result");
    resElement.nodeValue = this.Proc();
};