我需要在构造函数中获取所有函数名称,不包括那些分配this
有的函数:
var MyObject = function (arg1, arg2) {
this.arg1 = arg1;
this.arg2 = arg2;
// Get all functions, i.e. 'foo', excluding 'arg1' and 'arg2'
};
MyObject.prototype.foo = function() {}
我用过 Underscore.js,但没有运气。假设实际参数都是函数:
var MyObject = function (arg1, arg2) {
this.arg1 = arg1;
this.arg2 = arg2;
// Array of object function names, that is 'foo', 'arg1' and 'arg2'
var functions = _.functions(this);
// Loop over function names
_.each(functions, function (name) {}, this) {
// Function arguments contain this.name? Strict check ===
if(_.contains(arguments, this.name) {
functions = _.without(functions, name); // Remove this function
}
}
};
MyObject.prototype.foo = function() {}