Javascript Template Resources

Can you recommend good resources for using traits in javascript? After some searches, I mostly find articles about libraries that provide functionality, but I was interested to learn how to implement features without a library.

I came across this post about whether there are other approaches? Javascript traits

Any examples in the real world are also welcome.

Thank.

+3
javascript traits
Aug 08 '11 at 2:23
source share
4 answers

I would suggest something simple, according to:

  • Let features be defined as standard JavaScript objects.

    var equalsTrait = { eq: function(obj) { return this == obj }, neq: function(obj) { return ! this.eq(obj) } }; 
  • Write a function to extend this class with your traits (and bind it to a reasonable location in the global area):

     window.Traits = {}; Traits.addToClass = function(traits, cls) { for (var key in traits) { if (cls.prototype[key]) { alert("Class " + cls + " already has a method named " + key + "!"); } else { cls.prototype[key] = traits[key]; } } } 
  • Profit!

+3
Aug 08 2018-11-11T00:
source share

Some (limited) feature information in trait.js library

Not about Javascript, but a good article on inheritance systems and traits, β€œ Traits: a mechanism for fine-grained reuse .” A description of the implementation details can be found in " " Apply Features to the Smalltalk Collection Hierarchy " ". Other articles of this type are listed on this page .

+2
Mar 14 2018-12-12T00:
source share

Two articles that describe binary-based Mixin and Trait-based approaches for JavaScript include A New Look at JavaScript Mixins by Angus Croll in May 2011 and Many JavaScript Talents to Summarize Platform-Oriented Approaches, such as Features and Mixes since April 2014.

so long

Appendix I

see also:

  • stackoverflow.com :: Features in javascript
  • stackoverflow.com :: How to properly use mixins in Javascript

Appendix II

Since from time to time I seem to play with this question, I do not want to add any last thoughts to it ...

The library’s agnostic approach without much glue code (as mentioned above) only works for very fine-grained behavioral reuse units. Thus, until one gets into more than 1 or 2 easily resolved conflicts, templates based on, for example, Angus Croll Flight Mixins are the way to the next.

If it comes to real features, there must be a level of abstraction for him. This level (for example, provided as some kind of syntactic sugar such as DSL) should hide complexity, for example. compiling signs from signs or resolving conflicts when setting time (when the behavior of an object is tied to an object / type).

There are currently 3 examples in SO that, from my point of view, provide exactly what the OP requested ...

Any examples in the real world are also welcome.

  • stackoverflow.com :: Composts and Mixers in JS
  • stackoverflow.com :: Mixers for ES6 classes overflowing with babel
  • stackoverflow.com :: Refactoring legacy mixing hierarchies
  • stackoverflow.com :: Multiple Inheritance Using Classes
+2
May 20 '14 at 20:31
source share

You can use the function to implement characteristics without a library.

See a working example Traits + Inheritance

Thanks dbarbeau

 // Usage __traits(TargetClass, Trait1, Trait2, ...); // Mix multiple definitions as traits. Properties will be overwritten if names are duplicating. function __traits(mixtureTarget) { for(var a=1; a < arguments.length; ++a) { var mixin = arguments[a]; for (var p in mixin){ if (mixin.hasOwnProperty(p)){ mixtureTarget[p] = mixin[p]; } }; Object.getOwnPropertyNames(mixin.prototype).forEach( function(name) { mixtureTarget.prototype[name] = mixin.prototype[name]; }); }; }; 
0
Jan 26 '16 at 13:11
source share



All Articles