Can you use shorthand property assignment using 'this'?












1















I can do this:



class Temp {
constructor() {
this.foo = 'foo'
this.bar = 'bar'
}

getObj() {

let boo = 'boo'
return {
boo
}
}
}
console.log(new Temp().getObj())
//prints { boo: 'boo' }


So how can I do this:



class Temp {
constructor() {
this.foo = 'foo'
this.bar = 'bar'
}

getObj() {

return {
this.foo
}
}
}
console.log(new Temp().getObj())


Is there special syntax or is it not supported?










share|improve this question

























  • Why do you need another special syntax? to get rid of those nasty four (!!) characters in { foo: this.foo, } ?

    – Jonas Wilms
    Nov 13 '18 at 22:09











  • @Jonas Wilms consistency and lower risk of error? Imagine that this is just an example and I may have a huge list of key/value pairs.

    – u84six
    Nov 13 '18 at 22:10













  • ... at the cost of weird syntax mixes.

    – Jonas Wilms
    Nov 13 '18 at 22:11











  • wait... do you just want to clone the class or extract only certain values?

    – Jonas Wilms
    Nov 13 '18 at 22:12











  • Thanks for the edit!

    – Bergi
    Nov 13 '18 at 22:16
















1















I can do this:



class Temp {
constructor() {
this.foo = 'foo'
this.bar = 'bar'
}

getObj() {

let boo = 'boo'
return {
boo
}
}
}
console.log(new Temp().getObj())
//prints { boo: 'boo' }


So how can I do this:



class Temp {
constructor() {
this.foo = 'foo'
this.bar = 'bar'
}

getObj() {

return {
this.foo
}
}
}
console.log(new Temp().getObj())


Is there special syntax or is it not supported?










share|improve this question

























  • Why do you need another special syntax? to get rid of those nasty four (!!) characters in { foo: this.foo, } ?

    – Jonas Wilms
    Nov 13 '18 at 22:09











  • @Jonas Wilms consistency and lower risk of error? Imagine that this is just an example and I may have a huge list of key/value pairs.

    – u84six
    Nov 13 '18 at 22:10













  • ... at the cost of weird syntax mixes.

    – Jonas Wilms
    Nov 13 '18 at 22:11











  • wait... do you just want to clone the class or extract only certain values?

    – Jonas Wilms
    Nov 13 '18 at 22:12











  • Thanks for the edit!

    – Bergi
    Nov 13 '18 at 22:16














1












1








1








I can do this:



class Temp {
constructor() {
this.foo = 'foo'
this.bar = 'bar'
}

getObj() {

let boo = 'boo'
return {
boo
}
}
}
console.log(new Temp().getObj())
//prints { boo: 'boo' }


So how can I do this:



class Temp {
constructor() {
this.foo = 'foo'
this.bar = 'bar'
}

getObj() {

return {
this.foo
}
}
}
console.log(new Temp().getObj())


Is there special syntax or is it not supported?










share|improve this question
















I can do this:



class Temp {
constructor() {
this.foo = 'foo'
this.bar = 'bar'
}

getObj() {

let boo = 'boo'
return {
boo
}
}
}
console.log(new Temp().getObj())
//prints { boo: 'boo' }


So how can I do this:



class Temp {
constructor() {
this.foo = 'foo'
this.bar = 'bar'
}

getObj() {

return {
this.foo
}
}
}
console.log(new Temp().getObj())


Is there special syntax or is it not supported?







javascript ecmascript-6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 22:15







u84six

















asked Nov 13 '18 at 21:59









u84sixu84six

1,38611735




1,38611735













  • Why do you need another special syntax? to get rid of those nasty four (!!) characters in { foo: this.foo, } ?

    – Jonas Wilms
    Nov 13 '18 at 22:09











  • @Jonas Wilms consistency and lower risk of error? Imagine that this is just an example and I may have a huge list of key/value pairs.

    – u84six
    Nov 13 '18 at 22:10













  • ... at the cost of weird syntax mixes.

    – Jonas Wilms
    Nov 13 '18 at 22:11











  • wait... do you just want to clone the class or extract only certain values?

    – Jonas Wilms
    Nov 13 '18 at 22:12











  • Thanks for the edit!

    – Bergi
    Nov 13 '18 at 22:16



















  • Why do you need another special syntax? to get rid of those nasty four (!!) characters in { foo: this.foo, } ?

    – Jonas Wilms
    Nov 13 '18 at 22:09











  • @Jonas Wilms consistency and lower risk of error? Imagine that this is just an example and I may have a huge list of key/value pairs.

    – u84six
    Nov 13 '18 at 22:10













  • ... at the cost of weird syntax mixes.

    – Jonas Wilms
    Nov 13 '18 at 22:11











  • wait... do you just want to clone the class or extract only certain values?

    – Jonas Wilms
    Nov 13 '18 at 22:12











  • Thanks for the edit!

    – Bergi
    Nov 13 '18 at 22:16

















Why do you need another special syntax? to get rid of those nasty four (!!) characters in { foo: this.foo, } ?

– Jonas Wilms
Nov 13 '18 at 22:09





Why do you need another special syntax? to get rid of those nasty four (!!) characters in { foo: this.foo, } ?

– Jonas Wilms
Nov 13 '18 at 22:09













@Jonas Wilms consistency and lower risk of error? Imagine that this is just an example and I may have a huge list of key/value pairs.

– u84six
Nov 13 '18 at 22:10







@Jonas Wilms consistency and lower risk of error? Imagine that this is just an example and I may have a huge list of key/value pairs.

– u84six
Nov 13 '18 at 22:10















... at the cost of weird syntax mixes.

– Jonas Wilms
Nov 13 '18 at 22:11





... at the cost of weird syntax mixes.

– Jonas Wilms
Nov 13 '18 at 22:11













wait... do you just want to clone the class or extract only certain values?

– Jonas Wilms
Nov 13 '18 at 22:12





wait... do you just want to clone the class or extract only certain values?

– Jonas Wilms
Nov 13 '18 at 22:12













Thanks for the edit!

– Bergi
Nov 13 '18 at 22:16





Thanks for the edit!

– Bergi
Nov 13 '18 at 22:16












4 Answers
4






active

oldest

votes


















1














I guess what you actually look for is:



return { ...this };


or if you want to omit some properties:



 const { bar, ...take } = this;
return take;





share|improve this answer































    1














    No, this is not yet supported. You will have to go by {foo: this.foo}.



    However, there is a stage 1 proposal for shorthand property definition improvements that would allow you to write {this.foo} as an object literal.






    share|improve this answer
























    • hmmm ... there was always confusion between block statements and object literals, not sure if that proposal makes it even harder to distinguish ...

      – Jonas Wilms
      Nov 13 '18 at 22:19






    • 1





      @JonasWilms Normally it's pretty obvious that you're in an expression context - when you are reading the whole code not getting presented an excerpt that could be both. I'd really love this proposal to get accepted, I was wanting this for years…

      – Bergi
      Nov 13 '18 at 22:23



















    0














    If 'foo' is a public property you can do something like this:



    class Foo {
    constructor() {
    this.foo = 'foo';
    }
    }

    const { foo } = (new Foo());
    console.log(foo); // 'foo'


    https://repl.it/repls/FastForcefulMice






    share|improve this answer
























    • Hmm, now I feel like I need to change the title to my question. So was asking if you can use shorthand property names with 'this'.

      – u84six
      Nov 13 '18 at 22:09






    • 2





      @u84six You can't.

      – Baruch
      Nov 13 '18 at 22:11



















    0














    If you want to use destructuring, you'll need to extract the property to a const, and then use shorthand property names:






    class Temp {
    constructor() {
    this.foo = 'foo'
    this.bar = 'bar'
    }

    getObj() {
    const { foo } = this;

    return {
    foo
    }
    }
    }
    console.log(new Temp().getObj())








    share|improve this answer


























    • I don't believe that's destructuring in the way I was expecting. Notice in my example that the key is the same name of the var without having to type it in.

      – u84six
      Nov 13 '18 at 22:06











    • I've added an answer with destructuring.

      – Ori Drori
      Nov 13 '18 at 22:10






    • 2





      Yes, my question was confusing. I was getting shorthand properties mixed with destructuring (i.e. I'm fairly new to js)

      – u84six
      Nov 13 '18 at 22:12











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290147%2fcan-you-use-shorthand-property-assignment-using-this%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I guess what you actually look for is:



    return { ...this };


    or if you want to omit some properties:



     const { bar, ...take } = this;
    return take;





    share|improve this answer




























      1














      I guess what you actually look for is:



      return { ...this };


      or if you want to omit some properties:



       const { bar, ...take } = this;
      return take;





      share|improve this answer


























        1












        1








        1







        I guess what you actually look for is:



        return { ...this };


        or if you want to omit some properties:



         const { bar, ...take } = this;
        return take;





        share|improve this answer













        I guess what you actually look for is:



        return { ...this };


        or if you want to omit some properties:



         const { bar, ...take } = this;
        return take;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 22:13









        Jonas WilmsJonas Wilms

        56.6k42851




        56.6k42851

























            1














            No, this is not yet supported. You will have to go by {foo: this.foo}.



            However, there is a stage 1 proposal for shorthand property definition improvements that would allow you to write {this.foo} as an object literal.






            share|improve this answer
























            • hmmm ... there was always confusion between block statements and object literals, not sure if that proposal makes it even harder to distinguish ...

              – Jonas Wilms
              Nov 13 '18 at 22:19






            • 1





              @JonasWilms Normally it's pretty obvious that you're in an expression context - when you are reading the whole code not getting presented an excerpt that could be both. I'd really love this proposal to get accepted, I was wanting this for years…

              – Bergi
              Nov 13 '18 at 22:23
















            1














            No, this is not yet supported. You will have to go by {foo: this.foo}.



            However, there is a stage 1 proposal for shorthand property definition improvements that would allow you to write {this.foo} as an object literal.






            share|improve this answer
























            • hmmm ... there was always confusion between block statements and object literals, not sure if that proposal makes it even harder to distinguish ...

              – Jonas Wilms
              Nov 13 '18 at 22:19






            • 1





              @JonasWilms Normally it's pretty obvious that you're in an expression context - when you are reading the whole code not getting presented an excerpt that could be both. I'd really love this proposal to get accepted, I was wanting this for years…

              – Bergi
              Nov 13 '18 at 22:23














            1












            1








            1







            No, this is not yet supported. You will have to go by {foo: this.foo}.



            However, there is a stage 1 proposal for shorthand property definition improvements that would allow you to write {this.foo} as an object literal.






            share|improve this answer













            No, this is not yet supported. You will have to go by {foo: this.foo}.



            However, there is a stage 1 proposal for shorthand property definition improvements that would allow you to write {this.foo} as an object literal.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 '18 at 22:15









            BergiBergi

            367k58547876




            367k58547876













            • hmmm ... there was always confusion between block statements and object literals, not sure if that proposal makes it even harder to distinguish ...

              – Jonas Wilms
              Nov 13 '18 at 22:19






            • 1





              @JonasWilms Normally it's pretty obvious that you're in an expression context - when you are reading the whole code not getting presented an excerpt that could be both. I'd really love this proposal to get accepted, I was wanting this for years…

              – Bergi
              Nov 13 '18 at 22:23



















            • hmmm ... there was always confusion between block statements and object literals, not sure if that proposal makes it even harder to distinguish ...

              – Jonas Wilms
              Nov 13 '18 at 22:19






            • 1





              @JonasWilms Normally it's pretty obvious that you're in an expression context - when you are reading the whole code not getting presented an excerpt that could be both. I'd really love this proposal to get accepted, I was wanting this for years…

              – Bergi
              Nov 13 '18 at 22:23

















            hmmm ... there was always confusion between block statements and object literals, not sure if that proposal makes it even harder to distinguish ...

            – Jonas Wilms
            Nov 13 '18 at 22:19





            hmmm ... there was always confusion between block statements and object literals, not sure if that proposal makes it even harder to distinguish ...

            – Jonas Wilms
            Nov 13 '18 at 22:19




            1




            1





            @JonasWilms Normally it's pretty obvious that you're in an expression context - when you are reading the whole code not getting presented an excerpt that could be both. I'd really love this proposal to get accepted, I was wanting this for years…

            – Bergi
            Nov 13 '18 at 22:23





            @JonasWilms Normally it's pretty obvious that you're in an expression context - when you are reading the whole code not getting presented an excerpt that could be both. I'd really love this proposal to get accepted, I was wanting this for years…

            – Bergi
            Nov 13 '18 at 22:23











            0














            If 'foo' is a public property you can do something like this:



            class Foo {
            constructor() {
            this.foo = 'foo';
            }
            }

            const { foo } = (new Foo());
            console.log(foo); // 'foo'


            https://repl.it/repls/FastForcefulMice






            share|improve this answer
























            • Hmm, now I feel like I need to change the title to my question. So was asking if you can use shorthand property names with 'this'.

              – u84six
              Nov 13 '18 at 22:09






            • 2





              @u84six You can't.

              – Baruch
              Nov 13 '18 at 22:11
















            0














            If 'foo' is a public property you can do something like this:



            class Foo {
            constructor() {
            this.foo = 'foo';
            }
            }

            const { foo } = (new Foo());
            console.log(foo); // 'foo'


            https://repl.it/repls/FastForcefulMice






            share|improve this answer
























            • Hmm, now I feel like I need to change the title to my question. So was asking if you can use shorthand property names with 'this'.

              – u84six
              Nov 13 '18 at 22:09






            • 2





              @u84six You can't.

              – Baruch
              Nov 13 '18 at 22:11














            0












            0








            0







            If 'foo' is a public property you can do something like this:



            class Foo {
            constructor() {
            this.foo = 'foo';
            }
            }

            const { foo } = (new Foo());
            console.log(foo); // 'foo'


            https://repl.it/repls/FastForcefulMice






            share|improve this answer













            If 'foo' is a public property you can do something like this:



            class Foo {
            constructor() {
            this.foo = 'foo';
            }
            }

            const { foo } = (new Foo());
            console.log(foo); // 'foo'


            https://repl.it/repls/FastForcefulMice







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 '18 at 22:07









            BaruchBaruch

            1,5701018




            1,5701018













            • Hmm, now I feel like I need to change the title to my question. So was asking if you can use shorthand property names with 'this'.

              – u84six
              Nov 13 '18 at 22:09






            • 2





              @u84six You can't.

              – Baruch
              Nov 13 '18 at 22:11



















            • Hmm, now I feel like I need to change the title to my question. So was asking if you can use shorthand property names with 'this'.

              – u84six
              Nov 13 '18 at 22:09






            • 2





              @u84six You can't.

              – Baruch
              Nov 13 '18 at 22:11

















            Hmm, now I feel like I need to change the title to my question. So was asking if you can use shorthand property names with 'this'.

            – u84six
            Nov 13 '18 at 22:09





            Hmm, now I feel like I need to change the title to my question. So was asking if you can use shorthand property names with 'this'.

            – u84six
            Nov 13 '18 at 22:09




            2




            2





            @u84six You can't.

            – Baruch
            Nov 13 '18 at 22:11





            @u84six You can't.

            – Baruch
            Nov 13 '18 at 22:11











            0














            If you want to use destructuring, you'll need to extract the property to a const, and then use shorthand property names:






            class Temp {
            constructor() {
            this.foo = 'foo'
            this.bar = 'bar'
            }

            getObj() {
            const { foo } = this;

            return {
            foo
            }
            }
            }
            console.log(new Temp().getObj())








            share|improve this answer


























            • I don't believe that's destructuring in the way I was expecting. Notice in my example that the key is the same name of the var without having to type it in.

              – u84six
              Nov 13 '18 at 22:06











            • I've added an answer with destructuring.

              – Ori Drori
              Nov 13 '18 at 22:10






            • 2





              Yes, my question was confusing. I was getting shorthand properties mixed with destructuring (i.e. I'm fairly new to js)

              – u84six
              Nov 13 '18 at 22:12
















            0














            If you want to use destructuring, you'll need to extract the property to a const, and then use shorthand property names:






            class Temp {
            constructor() {
            this.foo = 'foo'
            this.bar = 'bar'
            }

            getObj() {
            const { foo } = this;

            return {
            foo
            }
            }
            }
            console.log(new Temp().getObj())








            share|improve this answer


























            • I don't believe that's destructuring in the way I was expecting. Notice in my example that the key is the same name of the var without having to type it in.

              – u84six
              Nov 13 '18 at 22:06











            • I've added an answer with destructuring.

              – Ori Drori
              Nov 13 '18 at 22:10






            • 2





              Yes, my question was confusing. I was getting shorthand properties mixed with destructuring (i.e. I'm fairly new to js)

              – u84six
              Nov 13 '18 at 22:12














            0












            0








            0







            If you want to use destructuring, you'll need to extract the property to a const, and then use shorthand property names:






            class Temp {
            constructor() {
            this.foo = 'foo'
            this.bar = 'bar'
            }

            getObj() {
            const { foo } = this;

            return {
            foo
            }
            }
            }
            console.log(new Temp().getObj())








            share|improve this answer















            If you want to use destructuring, you'll need to extract the property to a const, and then use shorthand property names:






            class Temp {
            constructor() {
            this.foo = 'foo'
            this.bar = 'bar'
            }

            getObj() {
            const { foo } = this;

            return {
            foo
            }
            }
            }
            console.log(new Temp().getObj())








            class Temp {
            constructor() {
            this.foo = 'foo'
            this.bar = 'bar'
            }

            getObj() {
            const { foo } = this;

            return {
            foo
            }
            }
            }
            console.log(new Temp().getObj())





            class Temp {
            constructor() {
            this.foo = 'foo'
            this.bar = 'bar'
            }

            getObj() {
            const { foo } = this;

            return {
            foo
            }
            }
            }
            console.log(new Temp().getObj())






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 13 '18 at 22:27

























            answered Nov 13 '18 at 22:03









            Ori DroriOri Drori

            75.9k138092




            75.9k138092













            • I don't believe that's destructuring in the way I was expecting. Notice in my example that the key is the same name of the var without having to type it in.

              – u84six
              Nov 13 '18 at 22:06











            • I've added an answer with destructuring.

              – Ori Drori
              Nov 13 '18 at 22:10






            • 2





              Yes, my question was confusing. I was getting shorthand properties mixed with destructuring (i.e. I'm fairly new to js)

              – u84six
              Nov 13 '18 at 22:12



















            • I don't believe that's destructuring in the way I was expecting. Notice in my example that the key is the same name of the var without having to type it in.

              – u84six
              Nov 13 '18 at 22:06











            • I've added an answer with destructuring.

              – Ori Drori
              Nov 13 '18 at 22:10






            • 2





              Yes, my question was confusing. I was getting shorthand properties mixed with destructuring (i.e. I'm fairly new to js)

              – u84six
              Nov 13 '18 at 22:12

















            I don't believe that's destructuring in the way I was expecting. Notice in my example that the key is the same name of the var without having to type it in.

            – u84six
            Nov 13 '18 at 22:06





            I don't believe that's destructuring in the way I was expecting. Notice in my example that the key is the same name of the var without having to type it in.

            – u84six
            Nov 13 '18 at 22:06













            I've added an answer with destructuring.

            – Ori Drori
            Nov 13 '18 at 22:10





            I've added an answer with destructuring.

            – Ori Drori
            Nov 13 '18 at 22:10




            2




            2





            Yes, my question was confusing. I was getting shorthand properties mixed with destructuring (i.e. I'm fairly new to js)

            – u84six
            Nov 13 '18 at 22:12





            Yes, my question was confusing. I was getting shorthand properties mixed with destructuring (i.e. I'm fairly new to js)

            – u84six
            Nov 13 '18 at 22:12


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290147%2fcan-you-use-shorthand-property-assignment-using-this%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly