How to work with Structs in JavaScript?

JavascriptObject Oriented ProgrammingFront End Technology

To work with Structs in JavaScript, you can try to run the following code −

Example

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <script>
         function displayStruct(str) {
            var str = str.split(' ');
            var count = str.length;

            function constructor() {
               for (var i = 0; i < count; i++) {
                  this[str[i]] = arguments[i];
               }
            }
            return constructor;
         }

         var Item = displayStruct("player country");

         var res = new Item('Sachin', 'India');
         alert("Country: "+res.country);
      </script>
   </body>
   
</html>
raja
Published on 27-Feb-2018 15:49:12
Advertisements