musictheory / NilScript

Objective-C-style language superset of JavaScript with a tiny, simple runtime
Other
50 stars 5 forks source link

Best way to figure out if target is actually a [thing class]; #91

Closed IngwiePhoenix closed 8 years ago

IngwiePhoenix commented 8 years ago

I use ES6/JSX now, but I am writing my controllers using OJ. So from within my app router, I want to verify if the obtained object is actually a OJ class, or at least instance.

Consider this example:

// App/Controller/MyController.oj
@implementation MyController : BIRD3BaseController
@end

module.exports = [MyController class];
// App/Entry/Browser/Router.js
import Grapnel from "grapnel";
import ControllerExecutor from "controller-executor";
var app = new Grapnel();

app.get("/{controller}/{action}", (req, e, next)=>{
    require.ensure([
        "BIRD3/App/Controller/"+req.controller
    ], function(require) {
        var controller = require("BIRD3/App/Controller/"+req.controller);
        try {
            if(/* controller actually is an OJ class */) {
                ControllerExecutor(controller, req, e, next);
            } else {
                var Controller = new controller(req);
                Controller[req.action](req, e, next);
            }
        } catch(e) { next(e); }
    }, "mvc");
});

As you see, I either have my classes written in JSX or OJ. So I need to see if the exported class is from OJ or JSX. What would be a good way to do so?

iccir commented 8 years ago
if (controller instanceof [BIRD3BaseController class]) {

}
iccir commented 8 years ago

Or if you just need to check if it's an oj object or class:

if (oj.isObject(controller)) {
    // True if controller is an oj instance, or an oj class
}
IngwiePhoenix commented 8 years ago

That second one was exactly what i wanted! :)