Search code examples
javascriptfunctionobjectnullreturn

Return 2 objects using an IIFE


I'm trying to return 2 objects using an IIFE. I cant find whats wrong here.

var UIController = (function(){

    return{
        getMinput: function(){
            return {
            mstaff1: document.querySelector('#mstaff1').value,
            mstaff2: document.querySelector('#mstaff2').value,
            mpda: document.querySelector('#mpda').value,
            mpos: document.querySelector('#mpos').value,
            mcash: document.querySelector('#mcash').value,
            mtotal: document.querySelector('#mtotal').value
            };
        }


        getMinput: function(){
            return {
            mstaff1: document.querySelector('#mstaff1').value,
            mstaff2: document.querySelector('#mstaff2').value,
            mpda: document.querySelector('#mpda').value,
            mpos: document.querySelector('#mpos').value,
            mcash: document.querySelector('#mcash').value,
            mtotal: document.querySelector('#mtotal').value
            };
        }   
    };

})();

Solution

  • Here you have one object. You can't have the same name for methods inside one object. Also, you need to add ',' after each method or prop. So do it like this:

    (function(){
    
        return{
            getMinput: function(){
                return {
                mstaff1: document.querySelector('#mstaff1').value,
                mstaff2: document.querySelector('#mstaff2').value,
                mpda: document.querySelector('#mpda').value,
                mpos: document.querySelector('#mpos').value,
                mcash: document.querySelector('#mcash').value,
                mtotal: document.querySelector('#mtotal').value
                };
            },
    
            getMinput1: function(){
                return {
                mstaff1: document.querySelector('#mstaff1').value,
                mstaff2: document.querySelector('#mstaff2').value,
                mpda: document.querySelector('#mpda').value,
                mpos: document.querySelector('#mpos').value,
                mcash: document.querySelector('#mcash').value,
                mtotal: document.querySelector('#mtotal').value
                };
            }   
        };
        })());
    
    OR if you really need two objects you can use array: 
    
    (function(){
        return [
            getMinput: function(){
                return {
                mstaff1: document.querySelector('#mstaff1').value,
                mstaff2: document.querySelector('#mstaff2').value,
                mpda: document.querySelector('#mpda').value,
                mpos: document.querySelector('#mpos').value,
                mcash: document.querySelector('#mcash').value,
                mtotal: document.querySelector('#mtotal').value
                };
            },
    
    
            getMinput1: function(){
                return {
                mstaff1: document.querySelector('#mstaff1').value,
                mstaff2: document.querySelector('#mstaff2').value,
                mpda: document.querySelector('#mpda').value,
                mpos: document.querySelector('#mpos').value,
                mcash: document.querySelector('#mcash').value,
                mtotal: document.querySelector('#mtotal').value
                };
            }   
        ];
      })
    ());