国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 徐闻县| 霍州市| 南郑县| 明水县| 霍邱县| 汶川县| 宣汉县| 房产| 浙江省| 十堰市| 肇庆市| 襄城县| 邳州市| 丰镇市| 涞源县| 玛沁县| 甘德县| 丰镇市| 罗山县| 阳曲县| 沭阳县| 平乐县| 邢台市| 博爱县| 积石山| 罗源县| 同仁县| 满洲里市| 边坝县| 庐江县| 东阳市| 宝坻区| 家居| 华亭县| 桂阳县| 肥乡县| 汝阳县| 中西区| 达拉特旗| 来安县| 安阳县|