Javascript 调用原型的所有方法?
给定一个像下面这样的类,我可以Javascript 调用原型的所有方法?,javascript,typescript,Javascript,Typescript,给定一个像下面这样的类,我可以console.log通过它的所有PropertyNames class Security { constructor(param: ParamType) { this.method1(param); ... this.methodN(param); } method1(param){...}; ... methodN(param){...}; } Object.getOwnPropertyNames(Security.
console.log
通过它的所有PropertyNames
class Security {
constructor(param: ParamType) {
this.method1(param);
...
this.methodN(param);
}
method1(param){...};
...
methodN(param){...};
}
Object.getOwnPropertyNames(Security.prototype).forEach( (value) => {
console.log('Method: ', value);
// value()?
});
我的问题是如何调用所有方法来代替
//value()?
您可以这样编写:
class Security {
method1(param) {
console.log("M1");
}
method2(param) {
console.log("M2");
}
}
function callAll() {
Object.getOwnPropertyNames(Security.prototype).forEach(value => {
if (value !== 'constructor' && typeof Security.prototype[value] === 'function')
this[value]();
});
}
callAll.apply(new Security())
你可以这样写:
class Security {
method1(param) {
console.log("M1");
}
method2(param) {
console.log("M2");
}
}
function callAll() {
Object.getOwnPropertyNames(Security.prototype).forEach(value => {
if (value !== 'constructor' && typeof Security.prototype[value] === 'function')
this[value]();
});
}
callAll.apply(new Security())
您是否创建了
安全性实例?您需要有一个对象才能调用其方法。是的,我创建了它。您是否创建了安全性实例?你需要有一个对象才能调用它的方法。是的,我让它创建了更多的问题,如果可以的话:是否可以区分一个方法有多少个参数?有点像这样//如果1参数将此[值](passedParam1);//如果2个参数为该[值](passedParam1,passedParam2)
在上面的代码中,如何调用函数或使用多少参数取决于您,如果您想实际了解函数是如何调用的,您可以检查参数
对象,如果可以的话,再问一个问题:是否可以区分一个方法有多少参数?有点像这样//如果1参数将此[值](passedParam1);//如果2个参数为该[值](passedParam1,passedParam2)
在上面的代码中,如何调用函数或使用多少参数取决于您自己,如果您想真正了解函数是如何调用的,可以检查参数
对象