1

I have a little library, here is fragment of a function i use

HashManager.prototype.on = function(hash, callback) {

    if(hash[0] == '#') { hash = hash.substr(1); }

    if( typeof this.observers[hash] !== 'object' ) {
        this.observers[hash] = [];
    }

    this.observers[hash].push(callback);

    return this.observers[hash][this.observers[hash].length-1];
};

here is my arr

    var ajaxURLS = {
    '#!o_kompanii': '/apages/about.html',
    '#!production': '/apages/production.html',
    '#!gde_kupit': '/apages/gde_kupit.html',
    '#!hozyaike_na_zametku': '/apages/hozyaike_na_zametku.html',
    '#!news': '/apages/news.html',
    '#!contacts': '/apages/contacts.html',
    '#!kurochka_po_zernyshku': '/apages/kurochka_po_zernyshku.html'

    };

and here is my loop

   var HM = new window.HashManager();
   $.each(ajaxURLS,function(hash, uri) {
        HM.on(hash, function(uri,hash) {
        console.log(uri);
        console.log(hash);
        });
  });

my issue is that console.log always print me a last key and value of my array how can i pass arguments correctly? thanks a lot, best regards

Ivan
  • 11
  • 2

1 Answers1

0

Javascript closure should help you:

var HM = new window.HashManager();
   $.each(ajaxURLS, function(_hash, _uri) {
        HM.on(_hash, (function(uri,hash) {
            console.log(uri);
            console.log(hash);
        })(_hash, _uri));
  });
Community
  • 1
  • 1
Maxim Zhukov
  • 10,060
  • 5
  • 44
  • 88