ESM exporting getters and setters.

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

ESM exporting getters and setters.

Michael J. Ryan
    // myFoo.mjs
    _hiddenFoo = false;
    export get foo() {
      return _hiddenFoo;
    }

    export set foo(value) {
      _hiddenFoo = !!value;
    }

    // main.mjs
    import {foo} from './myFoo.mjs';

Not sure if this has been discussed, but would be a nice feature to have in some cases... I know, total side effects and mutations, all the same, would be a nice to have in a few cases.

-- 
Michael J. Ryan
480-270-4509

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ESM exporting getters and setters.

Jordan Harband
The getter part is already how it works - you can `export let foo = false;` and then later `foo = true`; and then `export function setFoo(v) { foo = v; }`. Why is the getter/setter syntax a significant improvement over this?

On Thu, Sep 20, 2018 at 4:21 PM, Michael J. Ryan <[hidden email]> wrote:
    // myFoo.mjs
    _hiddenFoo = false;
    export get foo() {
      return _hiddenFoo;
    }

    export set foo(value) {
      _hiddenFoo = !!value;
    }

    // main.mjs
    import {foo} from './myFoo.mjs';

Not sure if this has been discussed, but would be a nice feature to have in some cases... I know, total side effects and mutations, all the same, would be a nice to have in a few cases.

-- 
Michael J. Ryan
480-270-4509

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ESM exporting getters and setters.

J Decker


On Thu, Sep 20, 2018 at 6:00 PM Jordan Harband <[hidden email]> wrote:
The getter part is already how it works - you can `export let foo = false;` and then later `foo = true`; and then `export function setFoo(v) { foo = v; }`. Why is the getter/setter syntax a significant improvement over this?

On Thu, Sep 20, 2018 at 4:21 PM, Michael J. Ryan <[hidden email]> wrote:

If you removed just the export; it wouldn't be valid code....
 
    // myFoo.mjs
    _hiddenFoo = false;
    /*export*/ get foo() {
      return _hiddenFoo;
    }

    /*export*/ set foo(value) {
      _hiddenFoo = !!value;
    }

 
    /*export*/ get foo() {
                   ^^^

SyntaxError: Unexpected identifier 

var hiddenFoo;
var foo = { 
   get foo() {
      return _hiddenFoo;
    },

    set foo(value) {
      _hiddenFoo = !!value;
    }
}
export {foo};
 
// --- main
 import {foo} from './zz.mjs';

console.log( "what is foo?", foo );

// -- output

what is foo? { foo: [Getter/Setter] }


 
Not sure if this has been discussed, but would be a nice feature to have in some cases... I know, total side effects and mutations, all the same, would be a nice to have in a few cases.

-- 
Michael J. Ryan
480-270-4509

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ESM exporting getters and setters.

J Decker


On Fri, Sep 21, 2018 at 1:13 PM J Decker <[hidden email]> wrote:


On Thu, Sep 20, 2018 at 6:00 PM Jordan Harband <[hidden email]> wrote:
The getter part is already how it works - you can `export let foo = false;` and then later `foo = true`; and then `export function setFoo(v) { foo = v; }`. Why is the getter/setter syntax a significant improvement over this?

On Thu, Sep 20, 2018 at 4:21 PM, Michael J. Ryan <[hidden email]> wrote:

If you removed just the export; it wouldn't be valid code....
 
    // myFoo.mjs
    _hiddenFoo = false;
    /*export*/ get foo() {
      return _hiddenFoo;
    }

    /*export*/ set foo(value) {
      _hiddenFoo = !!value;
    }

 
    /*export*/ get foo() {
                   ^^^

SyntaxError: Unexpected identifier 

var hiddenFoo;
var foo = { 
   get foo() {
      return _hiddenFoo;
    },

    set foo(value) {
      _hiddenFoo = !!value;
    }
}
export {foo};
I missed the point a little... would be something like 

const foofoo = foo.foo; // which doesn't result as a setter/getter reference, but the value...
export {foofoo} 


 
 
// --- main
 import {foo} from './zz.mjs';

console.log( "what is foo?", foo );

// -- output

what is foo? { foo: [Getter/Setter] }


 
Not sure if this has been discussed, but would be a nice feature to have in some cases... I know, total side effects and mutations, all the same, would be a nice to have in a few cases.

-- 
Michael J. Ryan
480-270-4509

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss