How would I set up a method to update passed properties of JavaScript object?











up vote
0
down vote

favorite












I'm looking to create a method for an object to update only the properties that are passed.



for example



function Car () {
this.color = 'yellow'
this.brand = 'bmw'
this.key = 0

this.update = function (o) {
this.key++
// maybe Object.assign(this, o) somehow ??
}
}


If I do Car.update({color:'red'}) I want to update the object but not overwrite the brand. I want to use something like Object.assign(this, o) but not sure how to assign it. Do I need to create a prototype? Car.prototype.update = ?










share|improve this question




















  • 2




    So what went wrong with Object.assign? It will update this if you pass that as first argument. No need to assign anything more.
    – trincot
    Nov 11 at 21:08












  • but i would have to assign it to something right? like this = Object.assign(this, o) but that's a no-go
    – Olgo
    Nov 11 at 21:24










  • You need to create a new Car() first
    – charlietfl
    Nov 11 at 21:35










  • @trincot Ah, i see. looking at the docs, i didn't realize it copies to the first parameter which is the target. I thought it had to return something which you would then save to a var. convert that to an answer. :)
    – Olgo
    Nov 11 at 21:35












  • OK, I have done so.
    – trincot
    Nov 11 at 21:42















up vote
0
down vote

favorite












I'm looking to create a method for an object to update only the properties that are passed.



for example



function Car () {
this.color = 'yellow'
this.brand = 'bmw'
this.key = 0

this.update = function (o) {
this.key++
// maybe Object.assign(this, o) somehow ??
}
}


If I do Car.update({color:'red'}) I want to update the object but not overwrite the brand. I want to use something like Object.assign(this, o) but not sure how to assign it. Do I need to create a prototype? Car.prototype.update = ?










share|improve this question




















  • 2




    So what went wrong with Object.assign? It will update this if you pass that as first argument. No need to assign anything more.
    – trincot
    Nov 11 at 21:08












  • but i would have to assign it to something right? like this = Object.assign(this, o) but that's a no-go
    – Olgo
    Nov 11 at 21:24










  • You need to create a new Car() first
    – charlietfl
    Nov 11 at 21:35










  • @trincot Ah, i see. looking at the docs, i didn't realize it copies to the first parameter which is the target. I thought it had to return something which you would then save to a var. convert that to an answer. :)
    – Olgo
    Nov 11 at 21:35












  • OK, I have done so.
    – trincot
    Nov 11 at 21:42













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm looking to create a method for an object to update only the properties that are passed.



for example



function Car () {
this.color = 'yellow'
this.brand = 'bmw'
this.key = 0

this.update = function (o) {
this.key++
// maybe Object.assign(this, o) somehow ??
}
}


If I do Car.update({color:'red'}) I want to update the object but not overwrite the brand. I want to use something like Object.assign(this, o) but not sure how to assign it. Do I need to create a prototype? Car.prototype.update = ?










share|improve this question















I'm looking to create a method for an object to update only the properties that are passed.



for example



function Car () {
this.color = 'yellow'
this.brand = 'bmw'
this.key = 0

this.update = function (o) {
this.key++
// maybe Object.assign(this, o) somehow ??
}
}


If I do Car.update({color:'red'}) I want to update the object but not overwrite the brand. I want to use something like Object.assign(this, o) but not sure how to assign it. Do I need to create a prototype? Car.prototype.update = ?







javascript object methods prototype






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 21:26

























asked Nov 11 at 21:04









Olgo

134




134








  • 2




    So what went wrong with Object.assign? It will update this if you pass that as first argument. No need to assign anything more.
    – trincot
    Nov 11 at 21:08












  • but i would have to assign it to something right? like this = Object.assign(this, o) but that's a no-go
    – Olgo
    Nov 11 at 21:24










  • You need to create a new Car() first
    – charlietfl
    Nov 11 at 21:35










  • @trincot Ah, i see. looking at the docs, i didn't realize it copies to the first parameter which is the target. I thought it had to return something which you would then save to a var. convert that to an answer. :)
    – Olgo
    Nov 11 at 21:35












  • OK, I have done so.
    – trincot
    Nov 11 at 21:42














  • 2




    So what went wrong with Object.assign? It will update this if you pass that as first argument. No need to assign anything more.
    – trincot
    Nov 11 at 21:08












  • but i would have to assign it to something right? like this = Object.assign(this, o) but that's a no-go
    – Olgo
    Nov 11 at 21:24










  • You need to create a new Car() first
    – charlietfl
    Nov 11 at 21:35










  • @trincot Ah, i see. looking at the docs, i didn't realize it copies to the first parameter which is the target. I thought it had to return something which you would then save to a var. convert that to an answer. :)
    – Olgo
    Nov 11 at 21:35












  • OK, I have done so.
    – trincot
    Nov 11 at 21:42








2




2




So what went wrong with Object.assign? It will update this if you pass that as first argument. No need to assign anything more.
– trincot
Nov 11 at 21:08






So what went wrong with Object.assign? It will update this if you pass that as first argument. No need to assign anything more.
– trincot
Nov 11 at 21:08














but i would have to assign it to something right? like this = Object.assign(this, o) but that's a no-go
– Olgo
Nov 11 at 21:24




but i would have to assign it to something right? like this = Object.assign(this, o) but that's a no-go
– Olgo
Nov 11 at 21:24












You need to create a new Car() first
– charlietfl
Nov 11 at 21:35




You need to create a new Car() first
– charlietfl
Nov 11 at 21:35












@trincot Ah, i see. looking at the docs, i didn't realize it copies to the first parameter which is the target. I thought it had to return something which you would then save to a var. convert that to an answer. :)
– Olgo
Nov 11 at 21:35






@trincot Ah, i see. looking at the docs, i didn't realize it copies to the first parameter which is the target. I thought it had to return something which you would then save to a var. convert that to an answer. :)
– Olgo
Nov 11 at 21:35














OK, I have done so.
– trincot
Nov 11 at 21:42




OK, I have done so.
– trincot
Nov 11 at 21:42












3 Answers
3






active

oldest

votes

















up vote
0
down vote



accepted










Object.assign mutates the first argument, so there is no need to assign the result of Object.assign() to something.



Object.assign(this, o);


...will work fine.






function Car () {
this.color = 'yellow'
this.brand = 'bmw'
this.key = 0

this.update = function (o) {
this.key++
Object.assign(this, o);
}
}

var car = new Car();
car.update({color: 'red'});
console.log(car);








share|improve this answer




























    up vote
    1
    down vote













    The idea you suggested in your OP works fine.






    function Car () {
    this.color = 'yellow';
    this.brand = 'bmw';
    this.key = 0;

    this.update = function (o) {
    Object.assign(this, o);
    }
    }

    a = new Car();
    a.update({color: "red"});
    console.log(a.color + " - " + a.brand + " - " + a.key);
    // returns "red - bmw - 0"








    share|improve this answer




























      up vote
      0
      down vote













      const object1 = {
      color : 'red'
      };

      const obj2 = Object.assign({color: 'blue', d: 5}, object1);
      console.log(obj2.color); // red


      Object.assign() method -> copy the values of all enumerable own properties.
      Finally It will return the target object






      share|improve this answer





















      • I just wanted it to be in a method, to keep it clean and so i can tie other functionality to it whenever there is an update, i updated my question to show what else I would like to tie in. to give a better idea of what i'm trying to accomplish
        – Olgo
        Nov 11 at 21:23













      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%2f53253226%2fhow-would-i-set-up-a-method-to-update-passed-properties-of-javascript-object%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote



      accepted










      Object.assign mutates the first argument, so there is no need to assign the result of Object.assign() to something.



      Object.assign(this, o);


      ...will work fine.






      function Car () {
      this.color = 'yellow'
      this.brand = 'bmw'
      this.key = 0

      this.update = function (o) {
      this.key++
      Object.assign(this, o);
      }
      }

      var car = new Car();
      car.update({color: 'red'});
      console.log(car);








      share|improve this answer

























        up vote
        0
        down vote



        accepted










        Object.assign mutates the first argument, so there is no need to assign the result of Object.assign() to something.



        Object.assign(this, o);


        ...will work fine.






        function Car () {
        this.color = 'yellow'
        this.brand = 'bmw'
        this.key = 0

        this.update = function (o) {
        this.key++
        Object.assign(this, o);
        }
        }

        var car = new Car();
        car.update({color: 'red'});
        console.log(car);








        share|improve this answer























          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          Object.assign mutates the first argument, so there is no need to assign the result of Object.assign() to something.



          Object.assign(this, o);


          ...will work fine.






          function Car () {
          this.color = 'yellow'
          this.brand = 'bmw'
          this.key = 0

          this.update = function (o) {
          this.key++
          Object.assign(this, o);
          }
          }

          var car = new Car();
          car.update({color: 'red'});
          console.log(car);








          share|improve this answer












          Object.assign mutates the first argument, so there is no need to assign the result of Object.assign() to something.



          Object.assign(this, o);


          ...will work fine.






          function Car () {
          this.color = 'yellow'
          this.brand = 'bmw'
          this.key = 0

          this.update = function (o) {
          this.key++
          Object.assign(this, o);
          }
          }

          var car = new Car();
          car.update({color: 'red'});
          console.log(car);








          function Car () {
          this.color = 'yellow'
          this.brand = 'bmw'
          this.key = 0

          this.update = function (o) {
          this.key++
          Object.assign(this, o);
          }
          }

          var car = new Car();
          car.update({color: 'red'});
          console.log(car);





          function Car () {
          this.color = 'yellow'
          this.brand = 'bmw'
          this.key = 0

          this.update = function (o) {
          this.key++
          Object.assign(this, o);
          }
          }

          var car = new Car();
          car.update({color: 'red'});
          console.log(car);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 21:41









          trincot

          115k1478109




          115k1478109
























              up vote
              1
              down vote













              The idea you suggested in your OP works fine.






              function Car () {
              this.color = 'yellow';
              this.brand = 'bmw';
              this.key = 0;

              this.update = function (o) {
              Object.assign(this, o);
              }
              }

              a = new Car();
              a.update({color: "red"});
              console.log(a.color + " - " + a.brand + " - " + a.key);
              // returns "red - bmw - 0"








              share|improve this answer

























                up vote
                1
                down vote













                The idea you suggested in your OP works fine.






                function Car () {
                this.color = 'yellow';
                this.brand = 'bmw';
                this.key = 0;

                this.update = function (o) {
                Object.assign(this, o);
                }
                }

                a = new Car();
                a.update({color: "red"});
                console.log(a.color + " - " + a.brand + " - " + a.key);
                // returns "red - bmw - 0"








                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  The idea you suggested in your OP works fine.






                  function Car () {
                  this.color = 'yellow';
                  this.brand = 'bmw';
                  this.key = 0;

                  this.update = function (o) {
                  Object.assign(this, o);
                  }
                  }

                  a = new Car();
                  a.update({color: "red"});
                  console.log(a.color + " - " + a.brand + " - " + a.key);
                  // returns "red - bmw - 0"








                  share|improve this answer












                  The idea you suggested in your OP works fine.






                  function Car () {
                  this.color = 'yellow';
                  this.brand = 'bmw';
                  this.key = 0;

                  this.update = function (o) {
                  Object.assign(this, o);
                  }
                  }

                  a = new Car();
                  a.update({color: "red"});
                  console.log(a.color + " - " + a.brand + " - " + a.key);
                  // returns "red - bmw - 0"








                  function Car () {
                  this.color = 'yellow';
                  this.brand = 'bmw';
                  this.key = 0;

                  this.update = function (o) {
                  Object.assign(this, o);
                  }
                  }

                  a = new Car();
                  a.update({color: "red"});
                  console.log(a.color + " - " + a.brand + " - " + a.key);
                  // returns "red - bmw - 0"





                  function Car () {
                  this.color = 'yellow';
                  this.brand = 'bmw';
                  this.key = 0;

                  this.update = function (o) {
                  Object.assign(this, o);
                  }
                  }

                  a = new Car();
                  a.update({color: "red"});
                  console.log(a.color + " - " + a.brand + " - " + a.key);
                  // returns "red - bmw - 0"






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 21:40









                  M. L.

                  261




                  261






















                      up vote
                      0
                      down vote













                      const object1 = {
                      color : 'red'
                      };

                      const obj2 = Object.assign({color: 'blue', d: 5}, object1);
                      console.log(obj2.color); // red


                      Object.assign() method -> copy the values of all enumerable own properties.
                      Finally It will return the target object






                      share|improve this answer





















                      • I just wanted it to be in a method, to keep it clean and so i can tie other functionality to it whenever there is an update, i updated my question to show what else I would like to tie in. to give a better idea of what i'm trying to accomplish
                        – Olgo
                        Nov 11 at 21:23

















                      up vote
                      0
                      down vote













                      const object1 = {
                      color : 'red'
                      };

                      const obj2 = Object.assign({color: 'blue', d: 5}, object1);
                      console.log(obj2.color); // red


                      Object.assign() method -> copy the values of all enumerable own properties.
                      Finally It will return the target object






                      share|improve this answer





















                      • I just wanted it to be in a method, to keep it clean and so i can tie other functionality to it whenever there is an update, i updated my question to show what else I would like to tie in. to give a better idea of what i'm trying to accomplish
                        – Olgo
                        Nov 11 at 21:23















                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      const object1 = {
                      color : 'red'
                      };

                      const obj2 = Object.assign({color: 'blue', d: 5}, object1);
                      console.log(obj2.color); // red


                      Object.assign() method -> copy the values of all enumerable own properties.
                      Finally It will return the target object






                      share|improve this answer












                      const object1 = {
                      color : 'red'
                      };

                      const obj2 = Object.assign({color: 'blue', d: 5}, object1);
                      console.log(obj2.color); // red


                      Object.assign() method -> copy the values of all enumerable own properties.
                      Finally It will return the target object







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 11 at 21:12









                      Nattamai Jawaharlal Manikandan

                      2068




                      2068












                      • I just wanted it to be in a method, to keep it clean and so i can tie other functionality to it whenever there is an update, i updated my question to show what else I would like to tie in. to give a better idea of what i'm trying to accomplish
                        – Olgo
                        Nov 11 at 21:23




















                      • I just wanted it to be in a method, to keep it clean and so i can tie other functionality to it whenever there is an update, i updated my question to show what else I would like to tie in. to give a better idea of what i'm trying to accomplish
                        – Olgo
                        Nov 11 at 21:23


















                      I just wanted it to be in a method, to keep it clean and so i can tie other functionality to it whenever there is an update, i updated my question to show what else I would like to tie in. to give a better idea of what i'm trying to accomplish
                      – Olgo
                      Nov 11 at 21:23






                      I just wanted it to be in a method, to keep it clean and so i can tie other functionality to it whenever there is an update, i updated my question to show what else I would like to tie in. to give a better idea of what i'm trying to accomplish
                      – Olgo
                      Nov 11 at 21:23




















                      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%2f53253226%2fhow-would-i-set-up-a-method-to-update-passed-properties-of-javascript-object%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

                      Bressuire

                      Vorschmack

                      Quarantine