javascriptjqueryvariablesexternalredefine

How to set/redefine variable in external js file


How can I re-define variable in external js file 'js/sample.js'?
The main idea was not touch core file and be possible to pass a new value (to var COUNT_DEFAULT) on js load from my new module.

core js example:

(function(_, $) { 

    var COUNT_DEFAULT = 2; 
    ...

Solution

  • Not sure if I understood you well, but you can do it like that.

    var module = (function (myVal) {
       var COUNT_DEFAULT = myVal || 2; 
       
       return {
          val: COUNT_DEFAULT
       };
    });
    
    var defaultVal = module();
    var customVal = module(45);
    
    console.log(defaultVal);
    console.log(customVal);