Javascript函数模块化方法初始化变量问题

时间:2019-02-27 10:50:08

标签: javascript initialization global revealing-module-pattern

我有一个模块化的libray javascript文件,其中要公开两个功能

  1. 初始化以初始化main.html文件中的变量。

  2. execValidation函数将基于通过主文件初始化的这三个变量集合运行。

例如:

var libraryModule = (function () {
  var arVals = {};
  var webFormData = {};
  var rules = [];

  function init(arVals, webFormData, rules) {
  //init all variables to global variables to use in execute Validations
    this.arVals = arVals;
    this.webFormData = webFormData;
    this.rules = rules;
  }

  //only passing RuleID, but it has dependencies of other variables, which I 
  //do not want to pass here
  function execValidation(ruleID) {
    //debugger;
    //Load arVals, webFormData and Rules from init
    var webFormData = this.webFormData;

    var arVals = this.arVals;
    var arVal = arVals[ruleID];

    var rules = this.rules;
    var rule = rules[ruleID]

    console.log(arVal);
    console.log(webFormData);
    console.log(rules);

  }

 return {
    execValidation: execValidation,
    init : init
 }
})(); // IIFE function

在我的html文件中,我这样呼叫

 var arVals = {};
    //calling json file using call back
    ruleConfigModule.init(function (data) {
        arVals = data;

    });

//passing the arVals, webFormData and rules collection to init
libraryModule.init(arVals, webFormData, rules);

Only passing the ruleID
var result = libraryModule.execValidation("Rule1");

我只想从execValidation函数传递一个变量,即RuleID,但是init函数应在js库本身中设置这些变量。任何人都不能帮忙,因为它不起作用,或者需要帮助重新组织它。

用于填充arVals的JSON调用方法

var ruleConfigModule = (function () {

function init(callback) {
    loadJSON(function (json) {
        callback(json);
    });
}
// Let's hide this function

function loadJSON(callback) {
    var xobj = new XMLHttpRequest();
    xobj.overrideMimeType("application/json");
    xobj.open('GET', 'http://localhost/test/config.json', true);
    xobj.onreadystatechange = function () {
        if (xobj.readyState == 4 && xobj.status == "200") {
            callback(JSON.parse(xobj.responseText));
        }
    };
    xobj.send();
}

return {
    //loadJSON: loadJSON,
    init: init
}

})();

  

已更新:   块引用   如何确保在调用init方法之前已填充arVals?

1 个答案:

答案 0 :(得分:0)

这应该有效

function LibraryModule() {
  var arVals = {};
  var webFormData = {};
  var rules = [];
}

LibraryModule.prototype.init = function init(arVals, webFormData, rules) {
  //init all variables to global variables to use in execute Validations
  this.arVals = arVals;
  this.webFormData = webFormData;
  this.rules = rules;
}

LibraryModule.prototype.execValidation = function execValidation(ruleID) {
  //debugger;
  //Load arVals, webFormData and Rules from init
  var webFormData = this.webFormData;

  var arVals = this.arVals;
  var arVal = arVals[ruleID];

  var rules = this.rules;
  var rule = rules[ruleID]

  console.log(arVal);
  console.log(webFormData);
  console.log(rules);

  return rules;

}


let libraryModule = new LibraryModule();

libraryModule.init({
  rule: 'TEST'
}, {
  rule: 'TEST'
}, {
  rule: 'TEST'
})
var result = libraryModule.execValidation("rule");
console.log(result);