Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find out the class name instanceof #12

Open
gausie opened this issue Jan 4, 2014 · 1 comment
Open

Find out the class name instanceof #12

gausie opened this issue Jan 4, 2014 · 1 comment

Comments

@gausie
Copy link

gausie commented Jan 4, 2014

Is there a way of finding what Fiber class a variable I have is an instance of?

@jakobo
Copy link
Contributor

jakobo commented Jan 8, 2014

While JavaScript doesn't natively allow for this (everything's a runtime variable), you may be able to solve what you are trying to do with some sort of class registry:

// the below has not been tested

// definition
var ClassRegistry;
(function() {
  var AsStatic = Fiber.extend(function() {
    return {
      init: function() {
        this.classes = [];
        this.lookup = {};
      },
      register: function(name, klass) {
        this.classes.push(klass);
        this.lookup[this.classes.length - 1] = name;
      },
      getInstance: function(klass) {
        for (var i = 0, len = this.classes.length; i < len; i++) {
          if (klass instanceof this.classes[i]) {
            return this.lookup[i];
          }
        }
      }
    };
  });
  ClassRegistry = new AsStatic();
}());

// usage
var MyClass = Fiber.extend(function() {
  // ...
});
ClassRegistry.register('MyClass', MyClass);

var myClass = new MyClass();

ClassRegistry.getInstance(myClass); // MyClass

In many ways, this may be better as you'll have better control of your use case. Fiber specifically avoids these kinds of features in order to keep the implementation as close to a syntactic sugar layer for native code as possible.

Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants