oop - Class enumeration setter, convention javascript -


hi have following class:

const myclass = function myclass() {   this.state = null; }  myclass.prototype.set = function(key, value) {     this[key] = value; } 

and following enum in other module:

const enumeration = {     active: 'active',     pending: 'pending',     done: 'done' } 

i know best practices / conventions handle setter enumerations properties (state here)

i this:

let myclass = new myclass(); myclass.set('state', enumeration.pending); 

but bit verbose, , need know myclass module , enum module.
other idea that:

const myclass function myclass() {     this.state = null; }  myclass.prototype.setpending = function() {     this.state = enumeration.pending; } 

and allow call myclass.setpending() don't know right naming of kind of setters.

first argue question not opinion-, needs-based. if preference-based, means have not encountered issues best practices established avoid. in mind, first best practice keep simple possible. how api evolves depends on application's specific needs , ability spot ones appropriate.

below example "evolution of code" of upload object web ftp client. note there lot of different possible paths. example's aim demonstrate best practices not matter of preference.

you start off this:

function upload(state) {   this.state = state || 'pending';   this.elem = document.createelement('div'); } new upload().state = 'active'; 

then realize need progressbar view update when state set, make specialized setter:

upload.prototype.setstate = function(value) {   this.state = value;   this.elem.classname = 'state-' + this.state; }; 

however later on decide need display notification, when state set 'done'. or perhaps find in need of shorthand method because have type upload.setstate('done') lot , find typing upload.setstatedone() easier (cf. jquery's $.get vs $.ajax).

upload.prototype.setstatedone = function() {   this.setstate('done');   // code execute when state changes 'done' }; 

note can prefer consistent api on convenience (e.g. prefer $.get on $.ajax because there significant overhead in using latter, prefer use jquery's $(elem).on('click',.. on $.click because prefill 'click' string).

there's still problem setter: if set invalid state, e.g. 'whatever', attach class 'state-whatever', update setstate:

upload.prototype.setstate = function(value) {   if (['active', 'pending', 'done'].indexof(value) > -1)     this.state = value;   this.elem.classname = 'state-' + this.state; }; 

problem is, states hard-coded in method , re-use them in other components, define states map , adjust setstate. in order avoid hard dependency, use dependency injection instead of referencing global states map inside constructor:

var states = {   active: 'active',   pending: 'pending',   done: 'done' };  function upload(state, states) {   this.states = states;   this.state = state || this.states.pending;   this.elem = document.createelement('div'); }  upload.prototype.setstate = function(value) {   var states = object.keys(this.states).map(function(key) {      return key.tolowercase();   });   if (states.indexof(value) > -1) {     this.state = value;   this.elem.classname = 'state-' + this.state; }; 

on sidenote, setter in basic form function sets property value on object. how specific make it, depends on how many parameters hardcode setter.

function createsetter(context, prop, val) {   switch (arguments.length) {      case 0:        return function(p, v) { this[p] = v; };      case 1:        return function(p, v) { this[p] = v; }.bind(context);      case 2:        return function(v) { this[prop] = v; }.bind(context);      case 3:        return function() { this[prop] = val; }.bind(context);   } }  var genericsetter = createsetter(); // prop/val, implicit context,     boundsetter = createsetter({}); // prop/val, fixed context     propsetter = createsetter({'hello': 'world'}, 'hello') // fixed prop, val, defined context     propvalsetter = createsetter({'hello': 'world'}, 'hello', 'dude') // fixed prop, fixed val, fixed context 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -