Is it possible to automatically stringify in Javascript?











up vote
6
down vote

favorite












I know I can create a toString() function on an object, so that every time it's printed or treated like a string it will first stringify the object with that function.



Is it possible to do that directly so I can use String object functions on the object?



var SomeObject = function(a, b){
this.a = a;
this.b = b
}

SomeObject.prototype.toString = function(){
return [ this.a, this.b ].join(' ')
}

var objInstance = new SomeObject('this', 'that');

console.log(objInstance + '') // this that
console.log(("" + objInstance).split('')) // [ 't', 'h', 'i', 's', ' ', 't', 'h', 'a', 't' ]
console.log(objInstance.split()) // error


Is it possible to do so that the object "behaves" like a string when a String function is called on it?



In other words, I'd like objInstance.split() to have the same result as ("" + objInstance).split(''), and also objInstance.length or objInstance.match(/something/) etc. etc.










share|improve this question




























    up vote
    6
    down vote

    favorite












    I know I can create a toString() function on an object, so that every time it's printed or treated like a string it will first stringify the object with that function.



    Is it possible to do that directly so I can use String object functions on the object?



    var SomeObject = function(a, b){
    this.a = a;
    this.b = b
    }

    SomeObject.prototype.toString = function(){
    return [ this.a, this.b ].join(' ')
    }

    var objInstance = new SomeObject('this', 'that');

    console.log(objInstance + '') // this that
    console.log(("" + objInstance).split('')) // [ 't', 'h', 'i', 's', ' ', 't', 'h', 'a', 't' ]
    console.log(objInstance.split()) // error


    Is it possible to do so that the object "behaves" like a string when a String function is called on it?



    In other words, I'd like objInstance.split() to have the same result as ("" + objInstance).split(''), and also objInstance.length or objInstance.match(/something/) etc. etc.










    share|improve this question


























      up vote
      6
      down vote

      favorite









      up vote
      6
      down vote

      favorite











      I know I can create a toString() function on an object, so that every time it's printed or treated like a string it will first stringify the object with that function.



      Is it possible to do that directly so I can use String object functions on the object?



      var SomeObject = function(a, b){
      this.a = a;
      this.b = b
      }

      SomeObject.prototype.toString = function(){
      return [ this.a, this.b ].join(' ')
      }

      var objInstance = new SomeObject('this', 'that');

      console.log(objInstance + '') // this that
      console.log(("" + objInstance).split('')) // [ 't', 'h', 'i', 's', ' ', 't', 'h', 'a', 't' ]
      console.log(objInstance.split()) // error


      Is it possible to do so that the object "behaves" like a string when a String function is called on it?



      In other words, I'd like objInstance.split() to have the same result as ("" + objInstance).split(''), and also objInstance.length or objInstance.match(/something/) etc. etc.










      share|improve this question















      I know I can create a toString() function on an object, so that every time it's printed or treated like a string it will first stringify the object with that function.



      Is it possible to do that directly so I can use String object functions on the object?



      var SomeObject = function(a, b){
      this.a = a;
      this.b = b
      }

      SomeObject.prototype.toString = function(){
      return [ this.a, this.b ].join(' ')
      }

      var objInstance = new SomeObject('this', 'that');

      console.log(objInstance + '') // this that
      console.log(("" + objInstance).split('')) // [ 't', 'h', 'i', 's', ' ', 't', 'h', 'a', 't' ]
      console.log(objInstance.split()) // error


      Is it possible to do so that the object "behaves" like a string when a String function is called on it?



      In other words, I'd like objInstance.split() to have the same result as ("" + objInstance).split(''), and also objInstance.length or objInstance.match(/something/) etc. etc.







      javascript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 hours ago

























      asked 3 hours ago









      simone

      1,37811324




      1,37811324
























          4 Answers
          4






          active

          oldest

          votes

















          up vote
          9
          down vote













          One option would be to return a Proxy that checks whether the property exists on String.prototype, and if it does, calls that property with the string that represents the object:






          var SomeObject = function(a, b) {
          this.a = a;
          this.b = b
          return new Proxy(this, {
          get: (_, prop) => {
          if (this[prop] !== undefined) {
          return this[prop];
          }
          if (String.prototype[prop]) {
          return String.prototype[prop].bind(this.a + ' ' + this.b);
          }
          }
          });
          }


          var objInstance = new SomeObject('this', 'that');

          console.log(objInstance.split());
          console.log(objInstance.trim());
          console.log(objInstance.includes('this'));
          console.log(objInstance.includes('somethingelse'));








          share|improve this answer





















          • This options is really good, +1
            – Just code
            2 hours ago












          • +1. Remark: this definition is only available in ecmascript 6 or later. Reference
            – devildelta
            2 hours ago










          • I would recommend to declare the handler object outside of the constructor function, for the same reason that you should define methods on the prototype. Then you'd also use the target argument instead of closing over this.
            – Bergi
            8 mins ago


















          up vote
          2
          down vote













          You can let your objects inherit from String so that all string methods become available:






          class SomeObject extends String {
          constructor(a, b) {
          super(a + " " + b);
          this.a = a;
          this.b = b;
          }
          }

          var obj = new SomeObject('this', 'that');
          console.log(obj.split(""));





          No need to use complicated Proxy solutions :-)



          All the String.prototype methods (except for .toString, .valueOf and [Symbol.iterator]) are "intentionally generic; [they do] not require that its this value be a String object. Therefore, [they] can be transferred to other kinds of objects for use as a method." You can call them on any value, they will coerce it to a string (using .toString() or .valueOf as usual).



          You don't even need to use ES6 class extends to inherit from builtins, it works in ES5 as well:






          function SomeObject(a, b) {
          this.a = a;
          this.b = b;
          }
          SomeObject.prototype = Object.create(String.prototype);
          SomeObject.prototype.constructor = SomeObject;
          SomeObject.prototype.toString = function() {
          return this.a + " " + this.b;
          };

          var obj = new SomeObject('this', 'that');
          console.log(obj.split(""));








          share|improve this answer






























            up vote
            1
            down vote













            One option to extend the SomeObject too, something like this.






            var SomeObject = function(a, b){
            this.a = a;
            this.b = b
            }

            SomeObject.prototype.toString = function(){
            return [ this.a, this.b ].join(' ')
            }

            SomeObject.prototype.split = function(){
            return this.toString().split('');
            }


            var objInstance = new SomeObject('this', 'that');

            console.log(objInstance + '') // this that
            //console.log(("" + objInstance).split('')) // [ 't', 'h', 'i', 's', ' ', 't', 'h', 'a', 't' ]
            console.log(objInstance.split())








            share|improve this answer





















            • Interesting thought. I was thinking about doing this programmatically by doing it for all functions - but is there a way to lost all functions of an object?
              – simone
              2 hours ago










            • @simone lost all functions of an object you mean? Do it only if you have custom requirements, doesn't makes sense if you do it for all the functions.
              – Just code
              2 hours ago












            • "lost" should have been "list" - list all functions of an object
              – simone
              2 hours ago










            • @simone to handle all the functions of an object, you can use certainperformance's solution.
              – Just code
              2 hours ago










            • I was wondering about performance in using Proxy. Same as in setting the prototype in a deleted solution. But both look quite interesting...
              – simone
              2 hours ago


















            up vote
            0
            down vote













            If I understand correctly then you can use spread syntax to achieve this






             let a = 'this'; let b = 'that'; 

            const merged = [...a, ...b];

            console.log({merged});








            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%2f53809965%2fis-it-possible-to-automatically-stringify-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
              9
              down vote













              One option would be to return a Proxy that checks whether the property exists on String.prototype, and if it does, calls that property with the string that represents the object:






              var SomeObject = function(a, b) {
              this.a = a;
              this.b = b
              return new Proxy(this, {
              get: (_, prop) => {
              if (this[prop] !== undefined) {
              return this[prop];
              }
              if (String.prototype[prop]) {
              return String.prototype[prop].bind(this.a + ' ' + this.b);
              }
              }
              });
              }


              var objInstance = new SomeObject('this', 'that');

              console.log(objInstance.split());
              console.log(objInstance.trim());
              console.log(objInstance.includes('this'));
              console.log(objInstance.includes('somethingelse'));








              share|improve this answer





















              • This options is really good, +1
                – Just code
                2 hours ago












              • +1. Remark: this definition is only available in ecmascript 6 or later. Reference
                – devildelta
                2 hours ago










              • I would recommend to declare the handler object outside of the constructor function, for the same reason that you should define methods on the prototype. Then you'd also use the target argument instead of closing over this.
                – Bergi
                8 mins ago















              up vote
              9
              down vote













              One option would be to return a Proxy that checks whether the property exists on String.prototype, and if it does, calls that property with the string that represents the object:






              var SomeObject = function(a, b) {
              this.a = a;
              this.b = b
              return new Proxy(this, {
              get: (_, prop) => {
              if (this[prop] !== undefined) {
              return this[prop];
              }
              if (String.prototype[prop]) {
              return String.prototype[prop].bind(this.a + ' ' + this.b);
              }
              }
              });
              }


              var objInstance = new SomeObject('this', 'that');

              console.log(objInstance.split());
              console.log(objInstance.trim());
              console.log(objInstance.includes('this'));
              console.log(objInstance.includes('somethingelse'));








              share|improve this answer





















              • This options is really good, +1
                – Just code
                2 hours ago












              • +1. Remark: this definition is only available in ecmascript 6 or later. Reference
                – devildelta
                2 hours ago










              • I would recommend to declare the handler object outside of the constructor function, for the same reason that you should define methods on the prototype. Then you'd also use the target argument instead of closing over this.
                – Bergi
                8 mins ago













              up vote
              9
              down vote










              up vote
              9
              down vote









              One option would be to return a Proxy that checks whether the property exists on String.prototype, and if it does, calls that property with the string that represents the object:






              var SomeObject = function(a, b) {
              this.a = a;
              this.b = b
              return new Proxy(this, {
              get: (_, prop) => {
              if (this[prop] !== undefined) {
              return this[prop];
              }
              if (String.prototype[prop]) {
              return String.prototype[prop].bind(this.a + ' ' + this.b);
              }
              }
              });
              }


              var objInstance = new SomeObject('this', 'that');

              console.log(objInstance.split());
              console.log(objInstance.trim());
              console.log(objInstance.includes('this'));
              console.log(objInstance.includes('somethingelse'));








              share|improve this answer












              One option would be to return a Proxy that checks whether the property exists on String.prototype, and if it does, calls that property with the string that represents the object:






              var SomeObject = function(a, b) {
              this.a = a;
              this.b = b
              return new Proxy(this, {
              get: (_, prop) => {
              if (this[prop] !== undefined) {
              return this[prop];
              }
              if (String.prototype[prop]) {
              return String.prototype[prop].bind(this.a + ' ' + this.b);
              }
              }
              });
              }


              var objInstance = new SomeObject('this', 'that');

              console.log(objInstance.split());
              console.log(objInstance.trim());
              console.log(objInstance.includes('this'));
              console.log(objInstance.includes('somethingelse'));








              var SomeObject = function(a, b) {
              this.a = a;
              this.b = b
              return new Proxy(this, {
              get: (_, prop) => {
              if (this[prop] !== undefined) {
              return this[prop];
              }
              if (String.prototype[prop]) {
              return String.prototype[prop].bind(this.a + ' ' + this.b);
              }
              }
              });
              }


              var objInstance = new SomeObject('this', 'that');

              console.log(objInstance.split());
              console.log(objInstance.trim());
              console.log(objInstance.includes('this'));
              console.log(objInstance.includes('somethingelse'));





              var SomeObject = function(a, b) {
              this.a = a;
              this.b = b
              return new Proxy(this, {
              get: (_, prop) => {
              if (this[prop] !== undefined) {
              return this[prop];
              }
              if (String.prototype[prop]) {
              return String.prototype[prop].bind(this.a + ' ' + this.b);
              }
              }
              });
              }


              var objInstance = new SomeObject('this', 'that');

              console.log(objInstance.split());
              console.log(objInstance.trim());
              console.log(objInstance.includes('this'));
              console.log(objInstance.includes('somethingelse'));






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 2 hours ago









              CertainPerformance

              72.5k143453




              72.5k143453












              • This options is really good, +1
                – Just code
                2 hours ago












              • +1. Remark: this definition is only available in ecmascript 6 or later. Reference
                – devildelta
                2 hours ago










              • I would recommend to declare the handler object outside of the constructor function, for the same reason that you should define methods on the prototype. Then you'd also use the target argument instead of closing over this.
                – Bergi
                8 mins ago


















              • This options is really good, +1
                – Just code
                2 hours ago












              • +1. Remark: this definition is only available in ecmascript 6 or later. Reference
                – devildelta
                2 hours ago










              • I would recommend to declare the handler object outside of the constructor function, for the same reason that you should define methods on the prototype. Then you'd also use the target argument instead of closing over this.
                – Bergi
                8 mins ago
















              This options is really good, +1
              – Just code
              2 hours ago






              This options is really good, +1
              – Just code
              2 hours ago














              +1. Remark: this definition is only available in ecmascript 6 or later. Reference
              – devildelta
              2 hours ago




              +1. Remark: this definition is only available in ecmascript 6 or later. Reference
              – devildelta
              2 hours ago












              I would recommend to declare the handler object outside of the constructor function, for the same reason that you should define methods on the prototype. Then you'd also use the target argument instead of closing over this.
              – Bergi
              8 mins ago




              I would recommend to declare the handler object outside of the constructor function, for the same reason that you should define methods on the prototype. Then you'd also use the target argument instead of closing over this.
              – Bergi
              8 mins ago












              up vote
              2
              down vote













              You can let your objects inherit from String so that all string methods become available:






              class SomeObject extends String {
              constructor(a, b) {
              super(a + " " + b);
              this.a = a;
              this.b = b;
              }
              }

              var obj = new SomeObject('this', 'that');
              console.log(obj.split(""));





              No need to use complicated Proxy solutions :-)



              All the String.prototype methods (except for .toString, .valueOf and [Symbol.iterator]) are "intentionally generic; [they do] not require that its this value be a String object. Therefore, [they] can be transferred to other kinds of objects for use as a method." You can call them on any value, they will coerce it to a string (using .toString() or .valueOf as usual).



              You don't even need to use ES6 class extends to inherit from builtins, it works in ES5 as well:






              function SomeObject(a, b) {
              this.a = a;
              this.b = b;
              }
              SomeObject.prototype = Object.create(String.prototype);
              SomeObject.prototype.constructor = SomeObject;
              SomeObject.prototype.toString = function() {
              return this.a + " " + this.b;
              };

              var obj = new SomeObject('this', 'that');
              console.log(obj.split(""));








              share|improve this answer



























                up vote
                2
                down vote













                You can let your objects inherit from String so that all string methods become available:






                class SomeObject extends String {
                constructor(a, b) {
                super(a + " " + b);
                this.a = a;
                this.b = b;
                }
                }

                var obj = new SomeObject('this', 'that');
                console.log(obj.split(""));





                No need to use complicated Proxy solutions :-)



                All the String.prototype methods (except for .toString, .valueOf and [Symbol.iterator]) are "intentionally generic; [they do] not require that its this value be a String object. Therefore, [they] can be transferred to other kinds of objects for use as a method." You can call them on any value, they will coerce it to a string (using .toString() or .valueOf as usual).



                You don't even need to use ES6 class extends to inherit from builtins, it works in ES5 as well:






                function SomeObject(a, b) {
                this.a = a;
                this.b = b;
                }
                SomeObject.prototype = Object.create(String.prototype);
                SomeObject.prototype.constructor = SomeObject;
                SomeObject.prototype.toString = function() {
                return this.a + " " + this.b;
                };

                var obj = new SomeObject('this', 'that');
                console.log(obj.split(""));








                share|improve this answer

























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  You can let your objects inherit from String so that all string methods become available:






                  class SomeObject extends String {
                  constructor(a, b) {
                  super(a + " " + b);
                  this.a = a;
                  this.b = b;
                  }
                  }

                  var obj = new SomeObject('this', 'that');
                  console.log(obj.split(""));





                  No need to use complicated Proxy solutions :-)



                  All the String.prototype methods (except for .toString, .valueOf and [Symbol.iterator]) are "intentionally generic; [they do] not require that its this value be a String object. Therefore, [they] can be transferred to other kinds of objects for use as a method." You can call them on any value, they will coerce it to a string (using .toString() or .valueOf as usual).



                  You don't even need to use ES6 class extends to inherit from builtins, it works in ES5 as well:






                  function SomeObject(a, b) {
                  this.a = a;
                  this.b = b;
                  }
                  SomeObject.prototype = Object.create(String.prototype);
                  SomeObject.prototype.constructor = SomeObject;
                  SomeObject.prototype.toString = function() {
                  return this.a + " " + this.b;
                  };

                  var obj = new SomeObject('this', 'that');
                  console.log(obj.split(""));








                  share|improve this answer














                  You can let your objects inherit from String so that all string methods become available:






                  class SomeObject extends String {
                  constructor(a, b) {
                  super(a + " " + b);
                  this.a = a;
                  this.b = b;
                  }
                  }

                  var obj = new SomeObject('this', 'that');
                  console.log(obj.split(""));





                  No need to use complicated Proxy solutions :-)



                  All the String.prototype methods (except for .toString, .valueOf and [Symbol.iterator]) are "intentionally generic; [they do] not require that its this value be a String object. Therefore, [they] can be transferred to other kinds of objects for use as a method." You can call them on any value, they will coerce it to a string (using .toString() or .valueOf as usual).



                  You don't even need to use ES6 class extends to inherit from builtins, it works in ES5 as well:






                  function SomeObject(a, b) {
                  this.a = a;
                  this.b = b;
                  }
                  SomeObject.prototype = Object.create(String.prototype);
                  SomeObject.prototype.constructor = SomeObject;
                  SomeObject.prototype.toString = function() {
                  return this.a + " " + this.b;
                  };

                  var obj = new SomeObject('this', 'that');
                  console.log(obj.split(""));








                  class SomeObject extends String {
                  constructor(a, b) {
                  super(a + " " + b);
                  this.a = a;
                  this.b = b;
                  }
                  }

                  var obj = new SomeObject('this', 'that');
                  console.log(obj.split(""));





                  class SomeObject extends String {
                  constructor(a, b) {
                  super(a + " " + b);
                  this.a = a;
                  this.b = b;
                  }
                  }

                  var obj = new SomeObject('this', 'that');
                  console.log(obj.split(""));





                  function SomeObject(a, b) {
                  this.a = a;
                  this.b = b;
                  }
                  SomeObject.prototype = Object.create(String.prototype);
                  SomeObject.prototype.constructor = SomeObject;
                  SomeObject.prototype.toString = function() {
                  return this.a + " " + this.b;
                  };

                  var obj = new SomeObject('this', 'that');
                  console.log(obj.split(""));





                  function SomeObject(a, b) {
                  this.a = a;
                  this.b = b;
                  }
                  SomeObject.prototype = Object.create(String.prototype);
                  SomeObject.prototype.constructor = SomeObject;
                  SomeObject.prototype.toString = function() {
                  return this.a + " " + this.b;
                  };

                  var obj = new SomeObject('this', 'that');
                  console.log(obj.split(""));






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 7 mins ago

























                  answered 22 mins ago









                  Bergi

                  361k57537858




                  361k57537858






















                      up vote
                      1
                      down vote













                      One option to extend the SomeObject too, something like this.






                      var SomeObject = function(a, b){
                      this.a = a;
                      this.b = b
                      }

                      SomeObject.prototype.toString = function(){
                      return [ this.a, this.b ].join(' ')
                      }

                      SomeObject.prototype.split = function(){
                      return this.toString().split('');
                      }


                      var objInstance = new SomeObject('this', 'that');

                      console.log(objInstance + '') // this that
                      //console.log(("" + objInstance).split('')) // [ 't', 'h', 'i', 's', ' ', 't', 'h', 'a', 't' ]
                      console.log(objInstance.split())








                      share|improve this answer





















                      • Interesting thought. I was thinking about doing this programmatically by doing it for all functions - but is there a way to lost all functions of an object?
                        – simone
                        2 hours ago










                      • @simone lost all functions of an object you mean? Do it only if you have custom requirements, doesn't makes sense if you do it for all the functions.
                        – Just code
                        2 hours ago












                      • "lost" should have been "list" - list all functions of an object
                        – simone
                        2 hours ago










                      • @simone to handle all the functions of an object, you can use certainperformance's solution.
                        – Just code
                        2 hours ago










                      • I was wondering about performance in using Proxy. Same as in setting the prototype in a deleted solution. But both look quite interesting...
                        – simone
                        2 hours ago















                      up vote
                      1
                      down vote













                      One option to extend the SomeObject too, something like this.






                      var SomeObject = function(a, b){
                      this.a = a;
                      this.b = b
                      }

                      SomeObject.prototype.toString = function(){
                      return [ this.a, this.b ].join(' ')
                      }

                      SomeObject.prototype.split = function(){
                      return this.toString().split('');
                      }


                      var objInstance = new SomeObject('this', 'that');

                      console.log(objInstance + '') // this that
                      //console.log(("" + objInstance).split('')) // [ 't', 'h', 'i', 's', ' ', 't', 'h', 'a', 't' ]
                      console.log(objInstance.split())








                      share|improve this answer





















                      • Interesting thought. I was thinking about doing this programmatically by doing it for all functions - but is there a way to lost all functions of an object?
                        – simone
                        2 hours ago










                      • @simone lost all functions of an object you mean? Do it only if you have custom requirements, doesn't makes sense if you do it for all the functions.
                        – Just code
                        2 hours ago












                      • "lost" should have been "list" - list all functions of an object
                        – simone
                        2 hours ago










                      • @simone to handle all the functions of an object, you can use certainperformance's solution.
                        – Just code
                        2 hours ago










                      • I was wondering about performance in using Proxy. Same as in setting the prototype in a deleted solution. But both look quite interesting...
                        – simone
                        2 hours ago













                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      One option to extend the SomeObject too, something like this.






                      var SomeObject = function(a, b){
                      this.a = a;
                      this.b = b
                      }

                      SomeObject.prototype.toString = function(){
                      return [ this.a, this.b ].join(' ')
                      }

                      SomeObject.prototype.split = function(){
                      return this.toString().split('');
                      }


                      var objInstance = new SomeObject('this', 'that');

                      console.log(objInstance + '') // this that
                      //console.log(("" + objInstance).split('')) // [ 't', 'h', 'i', 's', ' ', 't', 'h', 'a', 't' ]
                      console.log(objInstance.split())








                      share|improve this answer












                      One option to extend the SomeObject too, something like this.






                      var SomeObject = function(a, b){
                      this.a = a;
                      this.b = b
                      }

                      SomeObject.prototype.toString = function(){
                      return [ this.a, this.b ].join(' ')
                      }

                      SomeObject.prototype.split = function(){
                      return this.toString().split('');
                      }


                      var objInstance = new SomeObject('this', 'that');

                      console.log(objInstance + '') // this that
                      //console.log(("" + objInstance).split('')) // [ 't', 'h', 'i', 's', ' ', 't', 'h', 'a', 't' ]
                      console.log(objInstance.split())








                      var SomeObject = function(a, b){
                      this.a = a;
                      this.b = b
                      }

                      SomeObject.prototype.toString = function(){
                      return [ this.a, this.b ].join(' ')
                      }

                      SomeObject.prototype.split = function(){
                      return this.toString().split('');
                      }


                      var objInstance = new SomeObject('this', 'that');

                      console.log(objInstance + '') // this that
                      //console.log(("" + objInstance).split('')) // [ 't', 'h', 'i', 's', ' ', 't', 'h', 'a', 't' ]
                      console.log(objInstance.split())





                      var SomeObject = function(a, b){
                      this.a = a;
                      this.b = b
                      }

                      SomeObject.prototype.toString = function(){
                      return [ this.a, this.b ].join(' ')
                      }

                      SomeObject.prototype.split = function(){
                      return this.toString().split('');
                      }


                      var objInstance = new SomeObject('this', 'that');

                      console.log(objInstance + '') // this that
                      //console.log(("" + objInstance).split('')) // [ 't', 'h', 'i', 's', ' ', 't', 'h', 'a', 't' ]
                      console.log(objInstance.split())






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 2 hours ago









                      Just code

                      7,69642966




                      7,69642966












                      • Interesting thought. I was thinking about doing this programmatically by doing it for all functions - but is there a way to lost all functions of an object?
                        – simone
                        2 hours ago










                      • @simone lost all functions of an object you mean? Do it only if you have custom requirements, doesn't makes sense if you do it for all the functions.
                        – Just code
                        2 hours ago












                      • "lost" should have been "list" - list all functions of an object
                        – simone
                        2 hours ago










                      • @simone to handle all the functions of an object, you can use certainperformance's solution.
                        – Just code
                        2 hours ago










                      • I was wondering about performance in using Proxy. Same as in setting the prototype in a deleted solution. But both look quite interesting...
                        – simone
                        2 hours ago


















                      • Interesting thought. I was thinking about doing this programmatically by doing it for all functions - but is there a way to lost all functions of an object?
                        – simone
                        2 hours ago










                      • @simone lost all functions of an object you mean? Do it only if you have custom requirements, doesn't makes sense if you do it for all the functions.
                        – Just code
                        2 hours ago












                      • "lost" should have been "list" - list all functions of an object
                        – simone
                        2 hours ago










                      • @simone to handle all the functions of an object, you can use certainperformance's solution.
                        – Just code
                        2 hours ago










                      • I was wondering about performance in using Proxy. Same as in setting the prototype in a deleted solution. But both look quite interesting...
                        – simone
                        2 hours ago
















                      Interesting thought. I was thinking about doing this programmatically by doing it for all functions - but is there a way to lost all functions of an object?
                      – simone
                      2 hours ago




                      Interesting thought. I was thinking about doing this programmatically by doing it for all functions - but is there a way to lost all functions of an object?
                      – simone
                      2 hours ago












                      @simone lost all functions of an object you mean? Do it only if you have custom requirements, doesn't makes sense if you do it for all the functions.
                      – Just code
                      2 hours ago






                      @simone lost all functions of an object you mean? Do it only if you have custom requirements, doesn't makes sense if you do it for all the functions.
                      – Just code
                      2 hours ago














                      "lost" should have been "list" - list all functions of an object
                      – simone
                      2 hours ago




                      "lost" should have been "list" - list all functions of an object
                      – simone
                      2 hours ago












                      @simone to handle all the functions of an object, you can use certainperformance's solution.
                      – Just code
                      2 hours ago




                      @simone to handle all the functions of an object, you can use certainperformance's solution.
                      – Just code
                      2 hours ago












                      I was wondering about performance in using Proxy. Same as in setting the prototype in a deleted solution. But both look quite interesting...
                      – simone
                      2 hours ago




                      I was wondering about performance in using Proxy. Same as in setting the prototype in a deleted solution. But both look quite interesting...
                      – simone
                      2 hours ago










                      up vote
                      0
                      down vote













                      If I understand correctly then you can use spread syntax to achieve this






                       let a = 'this'; let b = 'that'; 

                      const merged = [...a, ...b];

                      console.log({merged});








                      share|improve this answer

























                        up vote
                        0
                        down vote













                        If I understand correctly then you can use spread syntax to achieve this






                         let a = 'this'; let b = 'that'; 

                        const merged = [...a, ...b];

                        console.log({merged});








                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          If I understand correctly then you can use spread syntax to achieve this






                           let a = 'this'; let b = 'that'; 

                          const merged = [...a, ...b];

                          console.log({merged});








                          share|improve this answer












                          If I understand correctly then you can use spread syntax to achieve this






                           let a = 'this'; let b = 'that'; 

                          const merged = [...a, ...b];

                          console.log({merged});








                           let a = 'this'; let b = 'that'; 

                          const merged = [...a, ...b];

                          console.log({merged});





                           let a = 'this'; let b = 'that'; 

                          const merged = [...a, ...b];

                          console.log({merged});






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 3 hours ago









                          diEcho

                          37k26123197




                          37k26123197






























                              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%2f53809965%2fis-it-possible-to-automatically-stringify-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

                              Ellipse (mathématiques)

                              Quarter-circle Tiles

                              Mont Emei