Myrthan Myrthan - 1 year ago 61
Javascript Question

How I can export functionnaly variable

I want to export variable
Code looks like:

let url = '';
module.exports = Object.freeze({
url: url

I would to have possibility to edit property that it can be accesses by dot.

ObjectWhichHasAboveCode.url = ""

Can I achieve it in any functional way? Override assign operator?

I tried;

1) url: () => url
2) url: {
value: url,
writable: true

Is it possible to achieve it somehow?

Second question:

Can I mix exporting Object.freeze methods together with let variable?

[EDIT] Syntax error

Answer Source

If I understand correctly what you are looking for is the module pattern. You want method implementations to be frozen on the object, but you do not want the state of the object to be entirely immutable.

Here's what could be done:

module.exports = (function () {
  var url;

  return Object.freeze({
    get url() { return url; },
    setUrl: function (value) { url = value; }  


The url would have to be set using theObject.setUrl('some url'), but all the other members would be immutable. The url could still be accessed using the dot notation (theObject.url).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download