SoFunction
Updated on 2025-03-01

JS method to implement simple router functions

This article describes the method of JS to implement simple router functions. Share it for your reference. The specific implementation method is as follows:

var wawa = {};
 = function(){
  function Router(){
  }
   = function(routemap, defaultFunc){
    var that = this, rule, func;
     = [];
     = defaultFunc;
    for (var rule in routemap) {
      if (!(rule)) continue;
      ({
        rule: new RegExp(rule, 'i'),
        func: routemap[rule]
      });       
    }
  };
   = function(){
    ();
    var hash = , route, matchResult;
    for (var routeIndex in ){
      route = [routeIndex];
      matchResult = ();
      if (matchResult){
        (window, (1));
        return; 
      }
    }
    ();
  };
  return Router;
}();
var router = new ();
({
  '#/list/(.*)/(.*)': function(cate, id){
      ('list', cate, id);
    },
  '#/show/(.*)': function(id){
      ('show', id); 
    }
}, function(){
  ('default router');
});
();

I hope this article will be helpful to everyone's JavaScript programming.