An API extention to Object which allows user to create object property whose value will be dependent on the object state. It is useful if the code execution has conditional flow. It can minimize the use of if statements in Javascript coding. The library is wrapped in AMD wrapper, thus it can be loaded by requirejs. It can also be loaded directly using script tag.
Pen Example: https://codepen.io/ishaananuraag/pen/GPXZNY
<script src="http://cdn.jsdelivr.net/gh/ishaananuraag/StateProperty.js/code/StateProperty.js"></script>
StateProperty.js provides defineStateProperty API on Javascript Object constructor.
Object.defineStateProperty(obj[, prop], descriptor);
- obj The object on which to define the property.
- prop (or valueAttribute) - The name or Symbol of the property to be defined. (default: 'value')
- descriptors The descriptors for the property being defined.
- values This is the actual object representing different states(as keys) and their values
- stateAttribute The name or Symbol of the property which will determine the value of the valueAttribute. (default: 'state')
- defaultState This is the default state for the property being defined to refer to. If the stateAttribute is already present in object, this has no effect.
var obj={};
Object.defineStateProperty(obj,'showGreeting',{
values:{
'en': function(){
console.log('Hello');
},
'fr': function(){
console.log('Bonjour');
},
},
stateAttribute: 'language',
defaultState: 'en',
});
obj.showGreeting();
obj.language='fr';
obj.showGreeting();
> Hello
> Bonjour