Skip to content

Instantly share code, notes, and snippets.

@rosshadden
Last active May 28, 2018 03:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rosshadden/5801965 to your computer and use it in GitHub Desktop.
Save rosshadden/5801965 to your computer and use it in GitHub Desktop.
Dilemma in trying to obtain method list of child class from the parent constructor.
// Let's call this fileA.js.
class Controller {
constructor() {
// This is the only way I can figure out how to do what I need, but it feels shitty.
var routes = [];
for (var route in this) {
if (typeof this[route] === "function") {
routes.push(route);
}
}
// This returns ["constructor"], which is technically true.
// This would work if I had access to User, but that's lame.
Object.getOwnPropertyNames(Controller.prototype);
}
}
module.exports = Controller;
////////////////////////////////////////////////////
// Let's call this fileB.js, which has access to the above class.
class User extends Controller {
// I would like to avoid this class having a constructor at all.
// constructor() {
// super(Object.getOwnPropertyNames(User.prototype));
// }
get() {}
"post list"(req, res) {
res.json([2, 4, 6, 8]);
}
test(req, res) {
res.view();
}
}
module.exports = User;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment