mardi 19 juillet 2016

variable in module pattern not assigned issue

I am new to javascript and module pattern. So I am trying to write the following small library

(function(window, $){
    'use strict';


    function myLibrary(){
        var myLib = {};
        var options= {};
        var map;

        //_options =  {'map':ol.map, 'geojson': testdata}
        myLib.init = function init(_options){
            options = _options;

            map = _options.map;
            console.log("Initializing  ...");
            console.log("map : "+ map);
            console.log("options : "+ options);
        }

        myLib.map = map;
        myLib.options = options;

        return myLib;
    }

    if(typeof (window.testLib) === 'undefined'){
        window.testLib = myLibrary();
    }

})(window, $);

I fisrt call init() function

testLib.init( {'map':ol.map, 'geojson': testdata})

The problem come when I try to access the map and the option

 testLib.map   // return undefined
 testLib.options   // reutrn {}

I made the assignement in init but it doesnt seems to work. the assignment made in init() for map and options seems to be local to the init()

How can I make the assignment the the map and options that I declare with var ??

Thanks for help

Aucun commentaire:

Enregistrer un commentaire