js实现 路由跳转和js中bind的实现

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <style>
            #display{width: 300px;height: 300px}
        </style>
    </head>
    <body>
    <div id="app">
        <p><a href="#/">home-first</a></p>
        <p><a href="#/home">home</a></p>
        <p><a href="#/contact">contact</a></p>
        <p><a href="#/news">news</a></p>
    </div>
    <div id="display">

    </div>
    <script type="text/javascript">
        function Router(){
            this.paths={};
            this.curPath='';
        }
        Router.prototype={
            path:function(str,callback){
                var func=callback||function(){};
                this.paths[str]=func;
            },
            refresh:function(){
                this.curPath=location.hash.slice(1)||'/home'
                this.paths[this.curPath]()
            },
            init:function(){
                window.addEventListener('load',this.refresh.bind(this),false)
                window.addEventListener('hashchange',this.refresh.bind(this),false)
            }
        }
        var display=document.getElementById('display');
        var r=new Router();
        r.path('/home',function(){
            display.innerHTML='home';
            display.style.background='yellow'
        })
        r.path('/contact',function(){
            display.innerText='contact';
            display.style.background='black'
        })
        r.path('/news',function(){
            display.innerText='news';
            display.style.background='green'
        })
        r.init()
    </script>
    </body>
</html>
  • 这里的关键事load和hashchange事件,通过监测路由变化而得到内容的变化
  • this.paths是存储页面内容的对象
  • this.curPath是存储当前路由

bind的实现

    function a(fn,context) {
        var args = Array.prototype.slice.call(arguments,2)
        return function() {
            var innerArgs = Array.prototype.slice.call(arguments);
            var finalArgs = args.concat(innerArgs)
            fn.apply(context,finalArgs)
        }
    }
    function main() {
        console.log(this.b)
    }
    Function.prototype.bind = function(context) {
        var that = this;
        var args = Array.prototype.slice.call(arguments,1)
        return function() {
            var innerArgs = Array.prototype.slice.call(arguments);
            var finalArgs = args.concat(innerArgs)
            that.apply(context,finalArgs)
        }
    }

js实现多次bind



var self = null
Function.prototype.bind = function() {
    if (self == null){
        self = this
    }
    var context = [].shift.call(arguments),
        args = [].slice.call(arguments);
    return function() {
        return self.apply(context, [].concat.call(args, [].slice.call(arguments)));
    }
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值