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 −


Live Demo

<!DOCTYPE html>
         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: ";
Updated on 30-Jul-2019 22:30:21