export with get/set option

# Michael J. Ryan (5 years ago)

I mentioned something similar to this before, but wanted to update with some more thoughts on why it could be useful... In my specific case, I have the following scenario...

I have a "base" script that loads build values into window.BASE, the application in question is deployed on multiple clients, so strings and other configuration values are different. The configuration project is built separately and the correct configuration is built with CI/CD. There are other configuration options that are loaded via the API that are client configurable. I know it's a bit different of a use case, but it can't be that unique. I also understand that it's a minefield of mutation, but it's pretty centrally controlled early, and simplifies a LOT of the rest of the application.

For example, I currently have the following in my "base.js" reference inside my project...

// ------- BEGIN JS const base = window.BASE || {}; // config from release

export const environment = base.environment || 'internal.dev'; export const api = base.api || 'api.app.dev-domain'; // ... other constants exported from base with defaults ....

// value loaded from API before the SPA UI takes over from a spinner export let petitionOptions = null; export const setPetitionOptions = options => { // client manipulation/integration here petitionOptions = options; }; // ------- END JS

It would be nice imho if I could do the following...

// ------- BEGIN JS let _petitionOptions; export { // no identification here, nested in object get petitionOptions() {...}, set petitionOptions(value) {...} };

// alternately export get petitionOptions() {...} export set petitionOptions(value) {...} // ------- END JS

Even more, would be the ability to do other exports via export { name:value } in an object. While the former method does work, I just think having a getter/setter interface would be an interesting flexibility... Not sure how difficult a change it would be to making import variables assignable and allowing the set transparently.

thoughts?

# Isiah Meadows (5 years ago)

I really like this idea, especially if you combined it with making existing exports implicitly get (to avoid breaking).


Isiah Meadows contact at isiahmeadows.com, www.isiahmeadows.com