国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JS實(shí)現(xiàn)簡單路由器功能的方法

2019-11-20 12:24:18
字體:
供稿:網(wǎng)友

本文實(shí)例講述了JS實(shí)現(xiàn)簡單路由器功能的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

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

希望本文所述對大家的javascript程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 定兴县| 精河县| 麻江县| 德化县| 屏南县| 肥乡县| 泸州市| 兴化市| 红安县| 琼中| 逊克县| 武穴市| 成武县| 石屏县| 泌阳县| 固始县| 滁州市| 建平县| 东乡族自治县| 西藏| 荆州市| 承德市| 岳普湖县| 万宁市| 涞水县| 天气| 铜川市| 子长县| 平南县| 广西| 灯塔市| 桦甸市| 谢通门县| 布拖县| 泰顺县| 观塘区| 嵊泗县| 莎车县| 拉萨市| 新邵县| 桑植县|