2019独角兽企业重金招聘Python工程师标准>>>
类似于这样的点击选中
Function.prototype.before = function( beforefn ){
var __self = this; // 保存原函数的引用
return function(){ // 返回包含了原函数和新函数的"代理"函数
beforefn.apply( this, arguments ); // 执行新函数,修正 this
return __self.apply( this, arguments ); // 执行原函数
}
};
Function.prototype.after = function( afterfn ){
var __self = this;
return function(){
var ret = __self.apply( this, arguments );
afterfn.apply( this, arguments );
return ret;
}
};
var func = function(){
console.log( 2 );
};
func = func.before(function(){
console.log( 1 );
}).after(function(){
console.log( 3 );
});
func();
栗子:
学会了当然要会用栗子
- 本地音乐
- 下载管理
- 我的音乐云盘
- 我的歌手
- 我的电台
每次点击li都要选中添加效果 离开的时候选中效果消失 离开的时候指的是点击其它li元素
var liDom = document.querySelectorAll('.recommend li'),cashDom = '';
Function.prototype.before = function (beforeFn) {var _self= this;return function () {beforeFn.apply(this,arguments);return _self.apply(this,arguments)};
};Function.prototype.after = function (afterFn) {var _self= this;return function () {var ret = _self.apply(this,arguments);afterFn.apply(this,arguments);return ret}
};
var fn = function (dom) {return funtion(){dom.classList.add('chooseActive'); cashDom = dom;}
};
Array.prototype.slice.apply(liDom).map(function (dom,index) {dom.addEventListener('click',function(){fn(dom);fn.before(function () { cashDom.classList.remove('chooseActive'); }); })
})