簡體   English   中英

Javascript訪問父函數變量

[英]Javascript accessing parent function variable

嗨,我有一個關於訪問父函數中變量的問題

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;
}

我如何訪問updateDatepicker內部的“ this.UpdateGraph()”函數,到目前為止,它說UpdateGraph和this.UpdateGraph不存在

您可以this上下文綁定到您的處理程序函數,以防止擁有額外的范圍變量:

$(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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM