JavaScript - module object syntax -


i seem have messed brackets, commas, semi-colons. highlighted incorrect in emacs. assume that's why doesn't work. seem have gotten lost in curly braces, commas , semi-colons. commented parts highlighed in red indicating syntax errors.

var mygallery = (function () {     var s;      return {          settings: {             data: json.parse(data),         filter: document.getelementbyid("filter"),         gallery: document.getelementbyid("gallery")         },          init: function() {             // kick things off             s = this.settings;         // default, call fillimages function 'all' tag         this.createfilters("all");         },         createfilters: function(tag) {         // load image data         // made imgs global couldn't access displayimg         imgs = this.settings.data.images;          // image tags , generate tag array         var tags = ["all"];          for(var = 0; < object.keys(imgs).length; i++) {         // check if tag in tags         if (tags.indexof(imgs[i].tag) > -1) {             // yes, is, don't add         } else {             // no, isn't, add 'tags'             tags.push(imgs[i].tag);         }         }         // create unordered list, assign class , append filter         var ul = document.createelement('ul');         ul.classlist.add("ul-bare");         this.settings.filter.appendchild(ul);           // iterate on array , append each element li         for(var = 0; < tags.length; i++) {         var li=document.createelement('li');         ul.appendchild(li);         li.innerhtml=tags[i];         li.onclick = (function(x){             return function() {             this.displayimg(tags[x]);             })(i);    // first ) highlighted in red         }     }, // both curly brace , comma highlighed in red      displayimg: function(filter) {         // add images #gallery         for(var = 0; < object.keys(imgs).length; i++) {         var div = document.createelement("img");         // if tage 'all', display images         if (filter === "all") {             div.src = imgs[i].src;             this.settings.gallery.appendchild(div);         } else {             // display images of category (tag argument)             if (imgs[i].tag === filter) {             div.src = imgs[i].src;             this.settings.gallery.appendchild(div);             }           }         }          }     }; // semi colon highlighted in red }());  mygallery.init(); 

edit: seems comes down closure. once i've commented out part, ok:

//li.onclick = (function(x){ //    return function() { //  this.displayimg(tags[x]); //    })(i); 

edit 2:

so i've got following:

li.onclick = (function(x) {         return function() {         console.log(tags[x]);         this.displayimg(tags[x]);         };     })(i); 

this works intended if want print tags[x], if add second statement this.displayimg...., error this.displayimg not function.

please advise.

thank you.

// ...  li.onclick = (function(x){     return (function() {         this.displayimg(tags[x]);     }) })(i);  // ... 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -