1 回答
TA贡献1836条经验 获得超5个赞
Function.prototype.before = function() {
for (var i=0; i<arguments.length; i++) {
if (typeof arguments[i] === 'function') {
arguments[i]()
}
}
this()
}
在chrome的控制台测试可以达到你想要的效果,,
不过可能会有兼容性问题
更新
好像没什么意思,不过还是折腾了一下
Function.prototype.before = function() {
this.beforeArrFunc = []
for (var i=0; i<arguments.length; i++) {
if (typeof arguments[i] === 'function') {
this.beforeArrFunc.push(arguments[i])
}
}
}
Function.prototype.execute = function() {
for (var i=0; i<this.beforeArrFunc.length; i++) {
this.beforeArrFunc[i]()
}
this()
}
var a = function() {
console.log(1)
}
a.before(function() { console.log(2) })
a.execute()
这样的缺点是,具体调用时需要另一个实例方法来辅助
好处就是也可以直接调用a(),这样不会调用到before function
添加回答
举报
