I don’t really get the function of ember.js’ reopenClass. I thought it added extra code to the Object’s prototype, so all instances of that Object would get the functionality that was added in a non static way. It does not do this however. It looks like it only adds code that can be statically executed. For instance. I have this code:
Logger = Ember.Object.extend({
log: function(thing) {
console.log(thing + ' wassup');
}
});
var logger = Logger.create();
logger.log("1, yo")
logger.reopen({
log: function(name) {
console.log(name + 'ghurt')
}
});
logger.log("2, yo")
Logger.reopenClass({
log: function(name) {
console.log(name + 'fresh')
}
});
logger.log("3, yo")
Logger.log("4, yo")
It outputs this:
1, yo wassup
2, yoghurt
3, yoghurt
4, yofresh
What I expected was this:
1, yo wassup
2, yoghurt
3, yofresh
4, undefined (I think)
So my question is: What does reopenClass do and when do I use it?
In general,
reopenadds methods and properties to instances whereasreopenClassadds methods and properties to classes.The corresponding tests are ember-runtime/tests/system/object/reopen_test.js and packages/ember-runtime/tests/system/object/reopenClass_test.js.
I’ve updated your code and added some comments, see http://jsfiddle.net/pangratz666/yWKBF/: