functionRouter(){this.routes={};this.currentUrl='';}Router.prototype.route=function(path,callback){this.routes[path]=callback||function(){};}Router.prototype.refresh=function(){this.currentUrl=location.hash.slice('1')||'/';this.routes[this.currentUrl]();}Router.prototype.init=function(){window.addEventListener('load',this.refresh.bind(this),false);window.addEventListener('hashchange',this.refresh.bind(this),false);}window.Router=newRouter();window.Router.init();varcontent=document.querySelector('body');//changePageanythingfunctionchangeBgColor(color){content.style.backgroundColor=color;}Router.route('/',function(){changeBgColor('red')})Router.route('/blue',function(){changeBgColor('blue')})Router.route('/green',function(){changeBgColor('green')})问题就在于window.addEventListener('load',this.refresh.bind(this),false);这句话中this.refresh.bind(this),弄晕了,换成this.refresh.apply(this)和call都会报错,有人能帮我来理清这个this指向的顺序吗?以及为什么不能使用apply和call
BIG阳
阿晨1998
相关分类