Differences in objects?












1















I've seen two ways to use objects, I would like to know what's the difference or it's just different syntax?



Option 1



body(data) = {
item1: data.val1;
item2: data.val2;
item3: data.val3;
}


Option 2



body(data) = {
item1 = data.val1,
item2 = data.val2,
item3 = data.val3
}

body.item1 = '';
body['item2'] = '';









share|improve this question























  • The first example is creating a JSON object, although it should have ',' NOT ';' after each member. The second isn't valid either.

    – SPlatten
    Nov 23 '18 at 10:09











  • Are you talking about defining the object or accessing the object? Your option 1 is not valid JS, which you can see by running it anywhere. Neither is the second one though. I have to wonder, where exactly have you seen these used?

    – Roope
    Nov 23 '18 at 10:09


















1















I've seen two ways to use objects, I would like to know what's the difference or it's just different syntax?



Option 1



body(data) = {
item1: data.val1;
item2: data.val2;
item3: data.val3;
}


Option 2



body(data) = {
item1 = data.val1,
item2 = data.val2,
item3 = data.val3
}

body.item1 = '';
body['item2'] = '';









share|improve this question























  • The first example is creating a JSON object, although it should have ',' NOT ';' after each member. The second isn't valid either.

    – SPlatten
    Nov 23 '18 at 10:09











  • Are you talking about defining the object or accessing the object? Your option 1 is not valid JS, which you can see by running it anywhere. Neither is the second one though. I have to wonder, where exactly have you seen these used?

    – Roope
    Nov 23 '18 at 10:09
















1












1








1








I've seen two ways to use objects, I would like to know what's the difference or it's just different syntax?



Option 1



body(data) = {
item1: data.val1;
item2: data.val2;
item3: data.val3;
}


Option 2



body(data) = {
item1 = data.val1,
item2 = data.val2,
item3 = data.val3
}

body.item1 = '';
body['item2'] = '';









share|improve this question














I've seen two ways to use objects, I would like to know what's the difference or it's just different syntax?



Option 1



body(data) = {
item1: data.val1;
item2: data.val2;
item3: data.val3;
}


Option 2



body(data) = {
item1 = data.val1,
item2 = data.val2,
item3 = data.val3
}

body.item1 = '';
body['item2'] = '';






javascript typescript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 10:02









DangurDangur

646




646













  • The first example is creating a JSON object, although it should have ',' NOT ';' after each member. The second isn't valid either.

    – SPlatten
    Nov 23 '18 at 10:09











  • Are you talking about defining the object or accessing the object? Your option 1 is not valid JS, which you can see by running it anywhere. Neither is the second one though. I have to wonder, where exactly have you seen these used?

    – Roope
    Nov 23 '18 at 10:09





















  • The first example is creating a JSON object, although it should have ',' NOT ';' after each member. The second isn't valid either.

    – SPlatten
    Nov 23 '18 at 10:09











  • Are you talking about defining the object or accessing the object? Your option 1 is not valid JS, which you can see by running it anywhere. Neither is the second one though. I have to wonder, where exactly have you seen these used?

    – Roope
    Nov 23 '18 at 10:09



















The first example is creating a JSON object, although it should have ',' NOT ';' after each member. The second isn't valid either.

– SPlatten
Nov 23 '18 at 10:09





The first example is creating a JSON object, although it should have ',' NOT ';' after each member. The second isn't valid either.

– SPlatten
Nov 23 '18 at 10:09













Are you talking about defining the object or accessing the object? Your option 1 is not valid JS, which you can see by running it anywhere. Neither is the second one though. I have to wonder, where exactly have you seen these used?

– Roope
Nov 23 '18 at 10:09







Are you talking about defining the object or accessing the object? Your option 1 is not valid JS, which you can see by running it anywhere. Neither is the second one though. I have to wonder, where exactly have you seen these used?

– Roope
Nov 23 '18 at 10:09














2 Answers
2






active

oldest

votes


















1














In your example there is no difference, but the array-like syntax will help you once you need to use a variable as the object property, let's say:



const foo={}
const prop= 'item4';

foo[prop] = 'something good'

alert(foo.item4)//Should alert "something good"





share|improve this answer































    0














    There is no difference, you can access properties on Objects via the dot notation or the square brackets notation. To be compliant to the coding standards (and on the why the brackets notation is useful) you should always use the dot notation to reference to properties; the only case in which you should be using the square brackets notation is when you want to reference to a property that is not hard coded but will be referenced at run time. (I.E. when you use a variable to know which property to get).






    share|improve this answer
























      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%2f53444480%2fdifferences-in-objects%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      In your example there is no difference, but the array-like syntax will help you once you need to use a variable as the object property, let's say:



      const foo={}
      const prop= 'item4';

      foo[prop] = 'something good'

      alert(foo.item4)//Should alert "something good"





      share|improve this answer




























        1














        In your example there is no difference, but the array-like syntax will help you once you need to use a variable as the object property, let's say:



        const foo={}
        const prop= 'item4';

        foo[prop] = 'something good'

        alert(foo.item4)//Should alert "something good"





        share|improve this answer


























          1












          1








          1







          In your example there is no difference, but the array-like syntax will help you once you need to use a variable as the object property, let's say:



          const foo={}
          const prop= 'item4';

          foo[prop] = 'something good'

          alert(foo.item4)//Should alert "something good"





          share|improve this answer













          In your example there is no difference, but the array-like syntax will help you once you need to use a variable as the object property, let's say:



          const foo={}
          const prop= 'item4';

          foo[prop] = 'something good'

          alert(foo.item4)//Should alert "something good"






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 10:08









          sheff2k1sheff2k1

          444416




          444416

























              0














              There is no difference, you can access properties on Objects via the dot notation or the square brackets notation. To be compliant to the coding standards (and on the why the brackets notation is useful) you should always use the dot notation to reference to properties; the only case in which you should be using the square brackets notation is when you want to reference to a property that is not hard coded but will be referenced at run time. (I.E. when you use a variable to know which property to get).






              share|improve this answer




























                0














                There is no difference, you can access properties on Objects via the dot notation or the square brackets notation. To be compliant to the coding standards (and on the why the brackets notation is useful) you should always use the dot notation to reference to properties; the only case in which you should be using the square brackets notation is when you want to reference to a property that is not hard coded but will be referenced at run time. (I.E. when you use a variable to know which property to get).






                share|improve this answer


























                  0












                  0








                  0







                  There is no difference, you can access properties on Objects via the dot notation or the square brackets notation. To be compliant to the coding standards (and on the why the brackets notation is useful) you should always use the dot notation to reference to properties; the only case in which you should be using the square brackets notation is when you want to reference to a property that is not hard coded but will be referenced at run time. (I.E. when you use a variable to know which property to get).






                  share|improve this answer













                  There is no difference, you can access properties on Objects via the dot notation or the square brackets notation. To be compliant to the coding standards (and on the why the brackets notation is useful) you should always use the dot notation to reference to properties; the only case in which you should be using the square brackets notation is when you want to reference to a property that is not hard coded but will be referenced at run time. (I.E. when you use a variable to know which property to get).







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 10:07









                  Alpe89Alpe89

                  18319




                  18319






























                      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%2f53444480%2fdifferences-in-objects%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







                      這個網誌中的熱門文章

                      Academy of Television Arts & Sciences

                      L'Équipe

                      1995 France bombings