CNode

js有没有什么方法可以在调用一个对象方法之前调用一个指定函数

Tty-bt发布于12 年前最后回复12 年前1 回复3748 浏览0 收藏

js有没有什么方法可以在调用一个对象方法之前调用一个指定函数 比如 , 我想在调用a,b,c方法之前都调用一次c方法

function obj(){
	this.a = function(){
		console.log("a");
	}
	this.b = function(){
		console.log("b");
	}
	this.c = function(){
		console.log("c");
	}
}
查看回复

回复 (1)

T
tulayang#1·12 年前

this.compose = function (f) { var args_c = Array.prototype.slice.apply(arguments, 1); var self = this; return function () { self.c.apply(self, args_c); f.apply(self, arguments); }; };

var o = new obj(); o.compose(o.a.bind(o), f函数的参数值1,f函数的参数值2, ...) (a函数的参数值1, a函数的参数值2, ...);

var m = function (x) { return x * 2; }; o.compose(m, 1)();

参与回复
登录后即可参与回复。登录