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

Difference between reopenClass and direct assignment? #445

Closed
pixelcort opened this issue Jan 26, 2012 · 5 comments
Closed

Difference between reopenClass and direct assignment? #445

pixelcort opened this issue Jan 26, 2012 · 5 comments

Comments

@pixelcort
Copy link

I'm curious as to the difference between

App.MyClass.reopenClass({key:'value'});

and

App.MyClass.key = 'value';

Is the former just a way to set a bunch of properties on the class, or does it actually do something more interesting?

@raycohen
Copy link
Contributor

using reopenClass will allow the property to survive to subclasses, and therefore also be accessible from the instance of a subclass

http://jsfiddle.net/raycohen/t7tPQ/

@pixelcort
Copy link
Author

Doesn't seem to work:

OuterClass=Em.Object.extend()
InnerClass=OuterClass.extend()
OuterClass.a=1
OuterClass.reopenClass({b:2})

now: InnerClass.a properly returns undefined, however InnerClass.b should return 2 but is returning undefined.

however, if I do this:

OuterClass=Em.Object.extend()
OuterClass.a=1
OuterClass.reopenClass({b:2})
InnerClass=OuterClass.extend()

now InnerClass.b properly returns 2.

Is it expected behavior that one needs to use reopenClass before subclassing, or is that a bug?

@wagenet
Copy link
Member

wagenet commented Jan 26, 2012

@pixelcort, what @raycohen is saying is that subclasses don't receive properties not defined via reopenClass. I've made it a bit more clear here: http://jsfiddle.net/t7tPQ/1/

It seems like you were expecting it to modify subclasses if you call reopenClass on the parent after subclasses are defined. This is not a behavior I would expect. Also, in your first example, I can't see how InnerClass.a would return 1. In my tests it doesn't: http://jsfiddle.net/t7tPQ/2/

I think that we've been able to demonstrate how reopenClass is different than defining directly. And I've confirmed that there is no bug in this behavior. As a result, I'm closing this.

@wagenet wagenet closed this as completed Jan 26, 2012
@pixelcort
Copy link
Author

@wagenet, I specifically said that InnerClass.a properly returns undefined. I was never expecting it to return 1. I also understood @raycohen.

I see now that the intended purpose of reopenClass is to be used before subclassing. I would have closed this issue but you apparently already closed it for me.

@wagenet
Copy link
Member

wagenet commented Jan 27, 2012

@pixelcort Sorry, I guess I misunderstood you. Glad we got it sorted out though.

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

No branches or pull requests

3 participants