Ember.js reopenClass是如何工作的?

时间:2012-04-22 15:42:50

标签: ember.js

我真的没有得到ember.js'reopenClass的功能。我认为它为Object的原型添加了额外的代码,因此该Object的所有实例都将获得以非静态方式添加的功能。但是,它没有这样做。看起来它只添加了可以静态执行的代码。例如。我有这段代码:

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")

输出:

1, yo wassup
2, yoghurt
3, yoghurt
4, yofresh

我的期望是:

1, yo wassup
2, yoghurt
3, yofresh
4, undefined (I think)

所以我的问题是:reopenClass做什么以及何时使用它?

2 个答案:

答案 0 :(得分:45)

通常,reopen会为实例添加方法和属性,而reopenClass会为添加方法和属性。

相应的测试是ember-runtime/tests/system/object/reopen_test.jspackages/ember-runtime/tests/system/object/reopenClass_test.js

我已更新您的代码并添加了一些评论,请参阅http://jsfiddle.net/pangratz666/yWKBF/

Logger = Ember.Object.extend({
    log: function(thing) {
        console.log(thing + ' wassup');
    }
});

var logger1 = Logger.create();
var logger2 = Logger.create();

// instances of Logger have a 'wassup' method
try { Logger.log("1, yo"); } catch (e) {} // Object (subclass of Ember.Object) has no method 'log'
logger1.log("1, yo"); // 1, yo wassup
logger2.log("1, yo"); // 1, yo wassup

console.log('----');

// overwrite log of concrete logger instance logger1
logger1.reopen({
    log: function(name) {
        console.log(name + ' ghurt');
    }
});

try { Logger.log("1, yo"); } catch (e) {} // Object (subclass of Ember.Object) has no method 'log'
logger1.log("2, yo"); // 2, yo ghurt
logger2.log("2, yo"); // 2, yo wassup

console.log('----');

// classes of Logger have a 'fresh' method
Logger.reopenClass({
    log: function(name) {
        console.log(name + ' fresh');
    }
});

Logger.log("3, yo"); // 3, yo fresh
logger1.log("3, yo"); // 3, yo ghurt
logger2.log("3, yo"); // 3, yo wassup

console.log('----');

// new* instances of Logger have from now on a 'dawg' method
// * this will likely change in the future so already existing instances will reopened too
Logger.reopen({
    log: function(name) {
        console.log(name + ' dawg');
    }
});

Logger.log("4, yo"); // 4, yo fresh
logger1.log("4, yo"); // 4, yo ghurt
logger2.log("4, yo"); // 4, yo wassup
Logger.create().log("4, yo"); // 4, yo dawg

console.log('----');

答案 1 :(得分:0)

reopen更改了原型,从而更改了类的实例

reopenClass更改构造函数本身,从而通过创建仅在类上可用的静态属性和函数来更改 >但不是在班级的任何实例上。

请注意,reopen引入的更改仅在调用.create()

后生效

基于doc的代码示例:

http://emberjs.com/api/classes/Ember.Application.html#method_reopen

MyObject = Ember.Object.extend({
  name: 'an object'
});

o = MyObject.create();
o.get('name'); // 'an object'

MyObject.reopen({
  say: function(msg){
    console.log(msg);
  }
})

try{
    o.say("hey");
} catch(e) {
    console.log(e); // o.say is not a function (...yet)
}
o2 = MyObject.create();
o2.say("hello"); // logs "hello"

o.say("goodbye"); // logs "goodbye"

http://emberjs.com/api/classes/Ember.Application.html#method_reopenClass

MyObject = Ember.Object.extend({
  name: 'an object'
});

MyObject.reopenClass({
  canBuild: false
});

MyObject.canBuild; // false
o = MyObject.create();
o.canBuild; // undefined