获取JavaScript类ES6 babel上的所有函数
我需要在类某物上发现函数“foo”,但它不起作用。这是因为我缺少使用类语法的技巧,还是因为babel或其他原因?我更愿意在任何可能的地方使用“类”,但发现对我来说更重要 有没有天才知道这个获取JavaScript类ES6 babel上的所有函数,javascript,ecmascript-6,babeljs,Javascript,Ecmascript 6,Babeljs,我需要在类某物上发现函数“foo”,但它不起作用。这是因为我缺少使用类语法的技巧,还是因为babel或其他原因?我更愿意在任何可能的地方使用“类”,但发现对我来说更重要 有没有天才知道这个 class Something { constructor() { } foo() { console.log("foo"); } } var something = { foo : function() { console.log
class Something {
constructor() {
}
foo() {
console.log("foo");
}
}
var something = {
foo : function() {
console.log("foo");
}
}
var result = Object.getOwnPropertyNames(new Something()).join(", ");
console.log("new Something() = %s", result);
result = Object.getOwnPropertyNames(Something.__proto__).join(", ");
console.log("Something.__proto__ = %s", result);
result = Object.getOwnPropertyNames(something).join(", ");
console.log("var something = %s", result);
产生:
new Something() =
Something.__proto__ = length, name, arguments, caller, constructor, bind, call, apply, toString
var something = foo
好的,以上是一个简单的问题,我想达到的目标是:
module.exports.toDto = (object) =>{
var result = {};
Object.getOwnPropertyNames(object.__proto__).map(k => k.match("get(.*)")).filter(m => m).forEach(m => {
var prop = m[1].toLowerCase();
var value = object[m[0]]();
result[prop] = value;
});
return result;
};
这允许我发现类的属性。万岁 这里很棘手。对象。proto不会列出所有内容。您应该使用object.prototype所有实例方法和object for static方法。