difference between using setters or methods in javascript











up vote
0
down vote

favorite












I am reading about getters and setters in javascript. I would like to know if there is a difference between this two ways of coding with and without setters



first way, without setters.



>obj1 = {
arr: ;
}
>obj1.arr.push('first')
>obj1.arr
[ 'first' ]


Second way, with setters.



>obj2 = {
set add(data) {
this.arr.push(data);
},
arr:
}
>obj2.add = 'first'
>obj2.arr
[ 'first' ]









share|improve this question






















  • @andy push() is a method of an array. instead of writing a method (i.e push2()) to illustrate better my question I just used that built in method.
    – FidelVe
    Nov 11 at 11:57















up vote
0
down vote

favorite












I am reading about getters and setters in javascript. I would like to know if there is a difference between this two ways of coding with and without setters



first way, without setters.



>obj1 = {
arr: ;
}
>obj1.arr.push('first')
>obj1.arr
[ 'first' ]


Second way, with setters.



>obj2 = {
set add(data) {
this.arr.push(data);
},
arr:
}
>obj2.add = 'first'
>obj2.arr
[ 'first' ]









share|improve this question






















  • @andy push() is a method of an array. instead of writing a method (i.e push2()) to illustrate better my question I just used that built in method.
    – FidelVe
    Nov 11 at 11:57













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am reading about getters and setters in javascript. I would like to know if there is a difference between this two ways of coding with and without setters



first way, without setters.



>obj1 = {
arr: ;
}
>obj1.arr.push('first')
>obj1.arr
[ 'first' ]


Second way, with setters.



>obj2 = {
set add(data) {
this.arr.push(data);
},
arr:
}
>obj2.add = 'first'
>obj2.arr
[ 'first' ]









share|improve this question













I am reading about getters and setters in javascript. I would like to know if there is a difference between this two ways of coding with and without setters



first way, without setters.



>obj1 = {
arr: ;
}
>obj1.arr.push('first')
>obj1.arr
[ 'first' ]


Second way, with setters.



>obj2 = {
set add(data) {
this.arr.push(data);
},
arr:
}
>obj2.add = 'first'
>obj2.arr
[ 'first' ]






javascript setter






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 11:32









FidelVe

51




51












  • @andy push() is a method of an array. instead of writing a method (i.e push2()) to illustrate better my question I just used that built in method.
    – FidelVe
    Nov 11 at 11:57


















  • @andy push() is a method of an array. instead of writing a method (i.e push2()) to illustrate better my question I just used that built in method.
    – FidelVe
    Nov 11 at 11:57
















@andy push() is a method of an array. instead of writing a method (i.e push2()) to illustrate better my question I just used that built in method.
– FidelVe
Nov 11 at 11:57




@andy push() is a method of an array. instead of writing a method (i.e push2()) to illustrate better my question I just used that built in method.
– FidelVe
Nov 11 at 11:57












4 Answers
4






active

oldest

votes

















up vote
1
down vote



accepted










The setter syntax in your example does not really prevent the client code to still add a value using the direct push call as in the first code block. So the difference is that you just added another way to do the same thing.



To make a fair comparison, you would have to define the same method in both alternatives: once as a normal method and once as a setter method, and then the difference is just the syntax how the argument is passed to the method, either with obj.add('first') or obj.add = 'first'.



In this actual case I would vote against the setter, because it gives the false impression that if you "assign" another value, the first assigned value is overwritten:



obj.add = 'first';
obj.add = 'second';


... but obviously this is not the case: both values exist in the object now.






share|improve this answer




























    up vote
    0
    down vote













    First, The set Syntax bind an object property to a defined function. In this particular example, there is no difference between two codes, but let's say for example you want to check if the value is negative before adding it to the array, so you can use set to Encapsulate that behavior.



    So basically, using setter is only to add additional encapsulated behavior to the functions of the objects.



    The way of accessing the array index called bracket notation. it is equal to dot notation, except the bracket notation allows you to set new properties to objects or arrays dynamically.



    Hope this help you.






    share|improve this answer




























      up vote
      0
      down vote













      I think difference only about "how it looks like". Using setters it's closest way for understanding for people who came to js from object oriented languages.






      share|improve this answer




























        up vote
        0
        down vote













        The getter/setter property is not the same as "normal" instance property, one is called "named data property", the other is called "named accessor property".



        Please let met quote below part of documents from the ECMAScript 5.1.



        https://www.ecma-international.org/ecma-262/5.1/#sec-8.10.1




        An Object is a collection of properties. Each property is either a
        named data property, a named accessor property, or an internal
        property:



        A named data property associates a name with an ECMAScript language
        value and a set of Boolean attributes.



        A named accessor property associates a name with one or two accessor
        functions, and a set of Boolean attributes. The accessor functions are
        used to store or retrieve an ECMAScript language value that is
        associated with the property.



        An internal property has no name and is not directly accessible via
        ECMAScript language operators. Internal properties exist purely for
        specification purposes.



        There are two kinds of access for named (non-internal) properties: get
        and put, corresponding to retrieval and assignment, respectively.




        And




        If the value of an attribute is not explicitly specified by this
        specification for a named property, the default value defined in Table
        7 is used.







        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',
          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%2f53248298%2fdifference-between-using-setters-or-methods-in-javascript%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








          up vote
          1
          down vote



          accepted










          The setter syntax in your example does not really prevent the client code to still add a value using the direct push call as in the first code block. So the difference is that you just added another way to do the same thing.



          To make a fair comparison, you would have to define the same method in both alternatives: once as a normal method and once as a setter method, and then the difference is just the syntax how the argument is passed to the method, either with obj.add('first') or obj.add = 'first'.



          In this actual case I would vote against the setter, because it gives the false impression that if you "assign" another value, the first assigned value is overwritten:



          obj.add = 'first';
          obj.add = 'second';


          ... but obviously this is not the case: both values exist in the object now.






          share|improve this answer

























            up vote
            1
            down vote



            accepted










            The setter syntax in your example does not really prevent the client code to still add a value using the direct push call as in the first code block. So the difference is that you just added another way to do the same thing.



            To make a fair comparison, you would have to define the same method in both alternatives: once as a normal method and once as a setter method, and then the difference is just the syntax how the argument is passed to the method, either with obj.add('first') or obj.add = 'first'.



            In this actual case I would vote against the setter, because it gives the false impression that if you "assign" another value, the first assigned value is overwritten:



            obj.add = 'first';
            obj.add = 'second';


            ... but obviously this is not the case: both values exist in the object now.






            share|improve this answer























              up vote
              1
              down vote



              accepted







              up vote
              1
              down vote



              accepted






              The setter syntax in your example does not really prevent the client code to still add a value using the direct push call as in the first code block. So the difference is that you just added another way to do the same thing.



              To make a fair comparison, you would have to define the same method in both alternatives: once as a normal method and once as a setter method, and then the difference is just the syntax how the argument is passed to the method, either with obj.add('first') or obj.add = 'first'.



              In this actual case I would vote against the setter, because it gives the false impression that if you "assign" another value, the first assigned value is overwritten:



              obj.add = 'first';
              obj.add = 'second';


              ... but obviously this is not the case: both values exist in the object now.






              share|improve this answer












              The setter syntax in your example does not really prevent the client code to still add a value using the direct push call as in the first code block. So the difference is that you just added another way to do the same thing.



              To make a fair comparison, you would have to define the same method in both alternatives: once as a normal method and once as a setter method, and then the difference is just the syntax how the argument is passed to the method, either with obj.add('first') or obj.add = 'first'.



              In this actual case I would vote against the setter, because it gives the false impression that if you "assign" another value, the first assigned value is overwritten:



              obj.add = 'first';
              obj.add = 'second';


              ... but obviously this is not the case: both values exist in the object now.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 11 at 11:50









              trincot

              115k1478109




              115k1478109
























                  up vote
                  0
                  down vote













                  First, The set Syntax bind an object property to a defined function. In this particular example, there is no difference between two codes, but let's say for example you want to check if the value is negative before adding it to the array, so you can use set to Encapsulate that behavior.



                  So basically, using setter is only to add additional encapsulated behavior to the functions of the objects.



                  The way of accessing the array index called bracket notation. it is equal to dot notation, except the bracket notation allows you to set new properties to objects or arrays dynamically.



                  Hope this help you.






                  share|improve this answer

























                    up vote
                    0
                    down vote













                    First, The set Syntax bind an object property to a defined function. In this particular example, there is no difference between two codes, but let's say for example you want to check if the value is negative before adding it to the array, so you can use set to Encapsulate that behavior.



                    So basically, using setter is only to add additional encapsulated behavior to the functions of the objects.



                    The way of accessing the array index called bracket notation. it is equal to dot notation, except the bracket notation allows you to set new properties to objects or arrays dynamically.



                    Hope this help you.






                    share|improve this answer























                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      First, The set Syntax bind an object property to a defined function. In this particular example, there is no difference between two codes, but let's say for example you want to check if the value is negative before adding it to the array, so you can use set to Encapsulate that behavior.



                      So basically, using setter is only to add additional encapsulated behavior to the functions of the objects.



                      The way of accessing the array index called bracket notation. it is equal to dot notation, except the bracket notation allows you to set new properties to objects or arrays dynamically.



                      Hope this help you.






                      share|improve this answer












                      First, The set Syntax bind an object property to a defined function. In this particular example, there is no difference between two codes, but let's say for example you want to check if the value is negative before adding it to the array, so you can use set to Encapsulate that behavior.



                      So basically, using setter is only to add additional encapsulated behavior to the functions of the objects.



                      The way of accessing the array index called bracket notation. it is equal to dot notation, except the bracket notation allows you to set new properties to objects or arrays dynamically.



                      Hope this help you.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 11 at 11:40









                      Nadeem Khoury

                      563316




                      563316






















                          up vote
                          0
                          down vote













                          I think difference only about "how it looks like". Using setters it's closest way for understanding for people who came to js from object oriented languages.






                          share|improve this answer

























                            up vote
                            0
                            down vote













                            I think difference only about "how it looks like". Using setters it's closest way for understanding for people who came to js from object oriented languages.






                            share|improve this answer























                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote









                              I think difference only about "how it looks like". Using setters it's closest way for understanding for people who came to js from object oriented languages.






                              share|improve this answer












                              I think difference only about "how it looks like". Using setters it's closest way for understanding for people who came to js from object oriented languages.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 11 at 11:41









                              FridmanChan

                              649




                              649






















                                  up vote
                                  0
                                  down vote













                                  The getter/setter property is not the same as "normal" instance property, one is called "named data property", the other is called "named accessor property".



                                  Please let met quote below part of documents from the ECMAScript 5.1.



                                  https://www.ecma-international.org/ecma-262/5.1/#sec-8.10.1




                                  An Object is a collection of properties. Each property is either a
                                  named data property, a named accessor property, or an internal
                                  property:



                                  A named data property associates a name with an ECMAScript language
                                  value and a set of Boolean attributes.



                                  A named accessor property associates a name with one or two accessor
                                  functions, and a set of Boolean attributes. The accessor functions are
                                  used to store or retrieve an ECMAScript language value that is
                                  associated with the property.



                                  An internal property has no name and is not directly accessible via
                                  ECMAScript language operators. Internal properties exist purely for
                                  specification purposes.



                                  There are two kinds of access for named (non-internal) properties: get
                                  and put, corresponding to retrieval and assignment, respectively.




                                  And




                                  If the value of an attribute is not explicitly specified by this
                                  specification for a named property, the default value defined in Table
                                  7 is used.







                                  share|improve this answer

























                                    up vote
                                    0
                                    down vote













                                    The getter/setter property is not the same as "normal" instance property, one is called "named data property", the other is called "named accessor property".



                                    Please let met quote below part of documents from the ECMAScript 5.1.



                                    https://www.ecma-international.org/ecma-262/5.1/#sec-8.10.1




                                    An Object is a collection of properties. Each property is either a
                                    named data property, a named accessor property, or an internal
                                    property:



                                    A named data property associates a name with an ECMAScript language
                                    value and a set of Boolean attributes.



                                    A named accessor property associates a name with one or two accessor
                                    functions, and a set of Boolean attributes. The accessor functions are
                                    used to store or retrieve an ECMAScript language value that is
                                    associated with the property.



                                    An internal property has no name and is not directly accessible via
                                    ECMAScript language operators. Internal properties exist purely for
                                    specification purposes.



                                    There are two kinds of access for named (non-internal) properties: get
                                    and put, corresponding to retrieval and assignment, respectively.




                                    And




                                    If the value of an attribute is not explicitly specified by this
                                    specification for a named property, the default value defined in Table
                                    7 is used.







                                    share|improve this answer























                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      The getter/setter property is not the same as "normal" instance property, one is called "named data property", the other is called "named accessor property".



                                      Please let met quote below part of documents from the ECMAScript 5.1.



                                      https://www.ecma-international.org/ecma-262/5.1/#sec-8.10.1




                                      An Object is a collection of properties. Each property is either a
                                      named data property, a named accessor property, or an internal
                                      property:



                                      A named data property associates a name with an ECMAScript language
                                      value and a set of Boolean attributes.



                                      A named accessor property associates a name with one or two accessor
                                      functions, and a set of Boolean attributes. The accessor functions are
                                      used to store or retrieve an ECMAScript language value that is
                                      associated with the property.



                                      An internal property has no name and is not directly accessible via
                                      ECMAScript language operators. Internal properties exist purely for
                                      specification purposes.



                                      There are two kinds of access for named (non-internal) properties: get
                                      and put, corresponding to retrieval and assignment, respectively.




                                      And




                                      If the value of an attribute is not explicitly specified by this
                                      specification for a named property, the default value defined in Table
                                      7 is used.







                                      share|improve this answer












                                      The getter/setter property is not the same as "normal" instance property, one is called "named data property", the other is called "named accessor property".



                                      Please let met quote below part of documents from the ECMAScript 5.1.



                                      https://www.ecma-international.org/ecma-262/5.1/#sec-8.10.1




                                      An Object is a collection of properties. Each property is either a
                                      named data property, a named accessor property, or an internal
                                      property:



                                      A named data property associates a name with an ECMAScript language
                                      value and a set of Boolean attributes.



                                      A named accessor property associates a name with one or two accessor
                                      functions, and a set of Boolean attributes. The accessor functions are
                                      used to store or retrieve an ECMAScript language value that is
                                      associated with the property.



                                      An internal property has no name and is not directly accessible via
                                      ECMAScript language operators. Internal properties exist purely for
                                      specification purposes.



                                      There are two kinds of access for named (non-internal) properties: get
                                      and put, corresponding to retrieval and assignment, respectively.




                                      And




                                      If the value of an attribute is not explicitly specified by this
                                      specification for a named property, the default value defined in Table
                                      7 is used.








                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 11 at 12:02









                                      user8510613

                                      868




                                      868






























                                          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.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • 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%2f53248298%2fdifference-between-using-setters-or-methods-in-javascript%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

                                          Florida Star v. B. J. F.

                                          Danny Elfman

                                          Lugert, Oklahoma