Last active
May 28, 2018 03:58
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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