加载中...

@mixes


描述:此对象混入了另一个对象中的所有成员。

语法

@mixes <OtherObjectPath>

概述

@mixes标签指示当前对象混入了OtherObjectPath对象的所有成员,被混入的对象就是一个@mixin。

例子

首先,我们用@mixin标签描述一个混入:

  1. /**
  2. * This provides methods used for event handling. It's not meant to
  3. * be used directly.
  4. *
  5. * @mixin
  6. */
  7. var Eventful = {
  8. /**
  9. * Register a handler function to be called whenever this event is fired.
  10. * @param {string} eventName - Name of the event.
  11. * @param {function(Object)} handler - The handler to call.
  12. */
  13. on: function(eventName, handler) {
  14. // code...
  15. },
  16. /**
  17. * Fire an event, causing all handlers for that event name to run.
  18. * @param {string} eventName - Name of the event.
  19. * @param {Object} eventData - The data provided to each handler.
  20. */
  21. fire: function(eventName, eventData) {
  22. // code...
  23. }
  24. };

现在,我们添加一个FormButton类,并且调用"mix"函数,将Eventful的所有功能混入到FormButton,这样FormButton也可以触发事件和监听了。我们使用@mixes标签,以表明FormButton混入了Eventful的功能。

例如,使用@mixes标签:

  1. /**
  2. * @constructor FormButton
  3. * @mixes Eventful
  4. */
  5. var FormButton = function() {
  6. // code...
  7. };
  8. FormButton.prototype.press = function() {
  9. this.fire('press', {});
  10. }
  11. mix(Eventful).into(FormButton.prototype);

还没有评论.