Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

object access in a function doesn't work

This is the code:

(function(Info, undefined) {
    var createInfoTableForFeature = function (obj) {
        var data2form = {};
        data2form.name = obj.name;
        data2form.state = obj.state;
        data2form.stateid=obj.stateId;
        data2form.city = obj.city;
        data2form.cityId=obj.cityId;
        data2form.sector = obj.sector;
        data2form.sectorId=obj.sectorId;
        data2form.municipality = obj.municipality;
        data2form.municipalityId=obj.municipalityId;
        data2form.parish = obj.parish;
        data2form.parishId = obj.parishId; 
        data2form.postcode = obj.postcode;
    }
    Info.copy2form = function(data){
        console.log(data);
    }
})(window.Info = window.Info || {});

When I call Info.copy2form(data2form), data2form is undefined

like image 255
Santiago Elvira Ramirez Avatar asked Jul 30 '26 19:07

Santiago Elvira Ramirez


1 Answers

You want data2form to be global, then you'll have to remove de var keyword before the declaration of the variable data2form to make it global.

If you want to make it accesible from everywhere but within Info container, then you can declare it like this:

Info.data2form = {};

and then call your function like this:

Info.copy2form(Info.data2form)
like image 139
danielrvt Avatar answered Aug 02 '26 09:08

danielrvt