简体   繁体   中英

Javascript accessing parent function variable

hi I have a Question about accessing variables that are in a parent function

var DateRangePicker = function (name, clickFunction) {
    this.context = name;
    this.UpdateGraph = clickFunction;
    this.FinalDate = function () {
        var Dates = $(this.context).find("[name=datepickerText]").val().split('-');
        return Dates[1];
    };
    this.InitialDate = function () {
        var Dates = $(this.context).find("[name=datepickerText]").val().split('-   ');
       return Dates[0];
    };
    $(this.context).find("[name=UpdateDatepicker]").click(function () {
        var pickerText = $(InnerContext).find('[name=datepickerText]');
        var dates = pickerText.val().split('-');
        UpdateGraph(InitialDate(), FinalDate());
        $(context).find("[name=Toogler]").click();
    });
    return this;
}

How can i access the "this.UpdateGraph()" function inside the updateDatepicker click so far it sais that UpdateGraph and this.UpdateGraph doesnt exists

You can bind the this context to your handler function, to keep from having an extra scope variable:

$(this.context).find("[name=UpdateDatepicker]").click(function () {
    var pickerText = $(this.InnerContext).find('[name=datepickerText]');
    var dates = pickerText.val().split('-');
    this.UpdateGraph(this.InitialDate(), this.FinalDate());
    $(this.context).find("[name=Toogler]").click();
}.bind(this));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM