Guest User

Untitled

a guest
Jul 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. goog.provide("samples.traits");
  2. goog.provide("samples.traits.HasValue");
  3. goog.provide("samples.traits.HasValueImpl");
  4. goog.provide("samples.traits.MyClass");
  5.  
  6. goog.require("goog.debug.Logger");
  7.  
  8. goog.scope(function() {
  9.  
  10. var ns = samples.traits;
  11.  
  12. /**
  13. * An interface for types that have a value
  14. *
  15. * @interface
  16. */
  17. ns.HasValue = function() {};
  18.  
  19. /**
  20. * @return {string}
  21. */
  22. ns.HasValue.prototype.getValue = function() {};
  23.  
  24. /**
  25. * @param {string} value
  26. */
  27. ns.HasValue.prototype.setValue = function(value) {};
  28.  
  29. /**
  30. * @implements { samples.traits.HasValue }
  31. *
  32. * @constructor
  33. */
  34. ns.HasValueImpl = function() {
  35. };
  36.  
  37. /**
  38. * @type {string}
  39. */
  40. ns.HasValueImpl.prototype.value_ = "default";
  41.  
  42. /** @inheritDoc */
  43. ns.HasValueImpl.prototype.getValue = function() {
  44. return this.value_;
  45. };
  46.  
  47. /** @inheritDoc */
  48. ns.HasValueImpl.prototype.setValue = function(value) {
  49. this.value_ = value;
  50. };
  51.  
  52. /**
  53. * @implements {samples.traits.HasValue}
  54. *
  55. * @constructor
  56. */
  57. ns.MyClass = function() {
  58. };
  59.  
  60. /** @inheritDoc */
  61. ns.MyClass.prototype.getValue = function(){};
  62.  
  63. /** @inheritDoc */
  64. ns.MyClass.prototype.setValue = function(value){};
  65.  
  66. /**
  67. * @return {string}
  68. */
  69. ns.MyClass.prototype.toString = function() {
  70. return "myclass: "+this.getValue();
  71. };
  72.  
  73. ns.logger = goog.debug.Logger.getLogger("samples.traits");
  74.  
  75. /**
  76. * @param {samples.traits.HasValue} obj
  77. */
  78. ns.verifyType = function(obj) {
  79. ns.logger.info(obj.getValue());
  80. ns.logger.info(obj);
  81. };
  82.  
  83. ns.mixin = function(baseObject, traits) {
  84. for(x = 0; x < traits.length; x++) {
  85. goog.object.extend(baseObject, traits[x].prototype);
  86. }
  87. };
  88.  
  89. /**
  90. * starting point
  91. */
  92. ns.start = function(sandbox) {
  93.  
  94. var myObject = new ns.MyClass;
  95.  
  96. myObject.setValue("first");
  97. ns.verifyType(myObject);
  98.  
  99. ns.mixin(myObject, [ns.HasValueImpl]);
  100.  
  101. myObject.setValue("whatup");
  102. ns.verifyType(myObject);
  103. };
  104.  
  105. goog.exportSymbol("samples.traits.start", ns.start);
  106.  
  107. });
Add Comment
Please, Sign In to add comment