gpt4 book ai didi

javascript - 从构造函数中获取对象的所有 JavaScript 函数,而不是使用 "this"分配的函数?

转载 作者:行者123 更新时间:2023-11-28 20:38:09 24 4
gpt4 key购买 nike

我需要获取构造函数内的所有函数名称,排除那些使用 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() {}

最佳答案

您要求原型(prototype)定义的所有函数:

_.functions(MyObject.prototype);

关于javascript - 从构造函数中获取对象的所有 JavaScript 函数,而不是使用 "this"分配的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14945753/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com