javascript - 变量未在同一 JavaScript 页面中从一个函数传递到另一个函数

标签 javascript jquery

当我在下面的代码中定义的initialize函数中运行console.log时,如下所示:

console.log("Checking for Value_Of_Process_Type in Initialize Function ! :" +Value_Of_Process_Type);
console.log("Checking for Todays Date in Initialize Function ! :" +TodaysDate);

我得到这两个变量的未定义。但是,我在 processEmployeeData 函数的点击处理程序中定义的以下 console.log 中看到了这两个变量的值:

console.log("Value_Of_Process_Type and Value_Of_TodaysDate are as follows :"+Value_Of_Process_Type+" and "+TodaysDate);

所以基本上,我的变量没有从函数 processEmployeeData 传递到 initialize 函数。我是否可以采取任何其他方法来实现这一目标,或者我正在做的事情有什么错误?

function EmployeePage() {
    var self = this;
    this.employeeData = function (employee_number_) {

    // Initialize the page
    this.initialize = function () {

        var employee_number = AppManager.selectedData["employee_number"];
        console.log("employee_number By calling page:" +employee_number);

        var Value_Of_Process_Type = AppManager.selectedData["my_page.Process_Type"];
        var TodaysDate = AppManager.selectedData["my_page.date_value"];

        console.log("Checking for Value_Of_Process_Type in Initialize Function ! :" +Value_Of_Process_Type);  //outputs undefined here
        console.log("Checking for Todays Date in Initialize Function ! :" +TodaysDate);     //outputs undefined here             
    };


    this.processEmployeeData = function (collection_) {

       $("#myPanel").on('rowclick',function(event){
           row = event.args.rowindex;
           datarow = $("#myPanel").jqxGrid('getrowdata', row);
           var response = JSON.stringify(datarow,null,10);
           var Process_Type = datarow["processType"];
           AppManager.selectData("my_page.Process_Type", Process_Type);
           var  Value_Of_Process_Type = AppManager.selectedData["my_page.Process_Type"];
           var TodaysDate = datarow["date_value"];

           AppManager.selectData("my_page.date_value", TodaysDate);

           var Value_Of_TodaysDate  = AppManager.selectedData["my_page.TodaysDate"];

           console.log("Value_Of_Process_Type and Value_Of_TodaysDate are as follows :"+Value_Of_Process_Type+" and "+TodaysDate); // Prints the values
       });
    };

最佳答案

Javascript 中的函数主要充当另一种类型的对象。因此,您可以将变量分配给函数;在您的情况下,将声明更改为 this.Value_Of_Process_Type=AppManager.selectedData["my_page.Process_Type"]; 应该允许您使用 this.Value_Of_Process_Type 在函数的其他位置访问它>.

关于javascript - 变量未在同一 JavaScript 页面中从一个函数传递到另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39050929/

相关文章:

javascript - 使 Bootstrap 响应基于父 div 的最佳方法?

javascript - 计算用户定义数字的百分比

javascript - 如何将事件监听器添加到 jEditable 输入?

javascript - foreach 内的 $timeout

javascript - 如何刷新 promise 链中的数据?

javascript - Accordion 向下滑动打破页面

php - 我为 strip <script></script> 标签创建了这个表单,但是这个表单不起作用

javascript - Angular 6 - 从 app.component.html 中删除数据并更新 View 中的更改

javascript - 迭代动态的嵌套对象

javascript - 尝试设置模型值时,ngModel.$setViewValue 不是函数