Reopening Class #3

Closed
AquiGorka opened this Issue Jan 21, 2013 · 2 comments

Projects

None yet

2 participants

@AquiGorka

First of all: great library! Neat and simple. Exactly what I needed.

Rather than an issue, this is more a request: does selfish implement a way to reopen a class (object) and add more methods to it (once I have an instance running)?
If so, could you help me out with an example?

  • If you want to know why, I'm thinking on an scenario where the app has certain functionality but after the users logs in I add methods to the object.

Cheers,
Gorka

Owner
Gozala commented Jan 21, 2013

First of all: great library! Neat and simple. Exactly what I needed.

Thanks for the compliment, but even though I thought it was fun exercise (mainly to attempt convincing TC39 that simplification instead of adding classes to language would be better), but I have moved on from it. Mainly due to
expression problem inherent to all inheritance based solutions. Solution I have being using for last year and still very happy with is method which I'd encourage to take a look instead and there is also a good introduction page here:
https://github.com/Gozala/method/wiki/Motivation

Rather than an issue, this is more a request: does selfish implement a way to reopen a class (object) and add more methods to it (once I have an instance running)?

I'm not sure I fully understand question but Assuming I do selfish freezes exemplar objects that are similar to classes that prevents mutation over them since most likely that would harm integrity of the program. Although instances of that exemplar (anything instantiated like foo.new()) are fully mutable. Only thing that may trip you up though is the that redefinition of properties on the instance that are present on the prototype chain isn't possible by write operation
bar.new = 3 that's just how ES5 decided to do it. Although properties with such names still can be defined via ES5 reflection APIs Object.defineProperty(bar, 'new', { value: 3, ... }).

If so, could you help me out with an example?

  • If you want to know why, I'm thinking on an scenario where the app has certain functionality but after the users logs in I add methods to the object.

I don't really know how to solve you're specific scenario, I personally don't like APIs that change under the hood
without me be aware why. Now if you still wanna do it and use this library for doing it, that's still possible by creating your own base exemplar that opts out from freeze:

var MyBase = Base.extend({
  extend: function() {
    return this.merge.apply(Object.create(this), arguments)
  }
})

And use MyBase to create exemplars and objects that you wish to mutate at runtime.

P.S.: You may also consider reading this http://www.sics.se/~joe/bluetail/vol1/v1_oo.html ;)

Very informative! thanks!

@Gozala Gozala closed this Dec 3, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment