javascript - Is there a better alternative to eval() for my function checking algorithm -


i have following code below designed check function exists @ anytime. this code not have errors currently. i'm using because javascript files load in arbitrary orders , necessary project done way. since javascript files separated in order run functions between them have store them in global variable , call function global variable.

example of javascript file: "checker.js" // checks if function exists!

       var funclist = {};        $(document).ready(function(){             funclist.requirejs =             function requirejs(requires,callback){                 console.log("requirejs starting requires below:");                 console.log(requires);                 var attempts = 0;                  attemptrequire();                  function attemptrequire(){                     $.each(requires, function (key, value){                         attempts++;                         console.log("checking:"+value+" attempts="+attempts);                         if (typeof eval(value) !== 'undefined'){                             callback(); //them run function                         }else{                             console.log("require js waiting...");                             settimeout(function(){attemptrequire();}, 100);//wait , try again every 100 milliseconds.                         }                     });                 }                  console.log("requirejs finished!...");             };         }); 

example of using global function: sample.js

$(document).ready(function(){     console.log("quote.js loading...");      requires = {         0 : "$.fn.mask"     };      funclist["requirejs"](requires, function(){         $("#partone_phone").mask("(999) 999-9999? x99999");         console.log("quote.js loaded!");     });  }); 

the idea if plugin 'masking' on jquery has not loaded yet not attempt run code once loaded will. attempting run .mask when plugin not loaded cause fields not have affect desired. i'm using jquery.mask example, apply anything.

i'm using jquery.mask however, because jquery function seems only way reference $.fn.mask

typically can test functions saying typeof functionnamehere !== 'undefined' , works, since jquery function if send string '$.fn.mask' doesn't work unless put eval() around it... know bad form!

does have better alternative using eval() make work?

also note:

requires = {         0 : $.fn.mask     }; 

trying send function directly without strings not work, because if it's undefined stays undefined time goes requirejs.

a better alternative may pass function executed instead returns method looking or false.

requires = {     0 : function () {         return $ && $.fn && $.fn.mask;     } }; 

now, have call requires[0]() see if $.fn.mask defined.


side note: referencing "requirejs" might confusing future developers since isn't requirejs, it's own function checking if script done loading. requirejs module loader make problem less of problem if used it.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -