javascript - Scope issue inside a custom object -
i think having scope visibility issue can't figure out exactly: when log variable displayatonce right result, try use buttons nothing in return. have tried log this.navbuttons empty set... don't what's wrong code.
<!-- html code --> <div id="nav"> <a href="#" data-dir="prev">previous</a> <a href="#" data-dir="next">next</a> </div> /* js script jquery */ (function() { var newsnavigator = { init: function(config) { this.navbuttons = config.navbuttons; this.displayatonce = config.displayatonce; this.counter = 0; this.shownews(); this.enablenav(); }, shownews: function() { console.log(this.displayatonce); }, enablenav: function() { console.log(this.navbuttons); this.navbuttons.on('click', function() { console.log("clicked"); }); } }; newsnavigator.init({ displayatonce: 3, navbuttons: $('div#nav').find('a') }); })();
that happening because using (function())(); executes function immediately, maybe it's running code before dom ready
everything working fine in below demo
put code inside document ready or @ least call initialize method inside doc ready block like
$(function(){ newsnavigator.init({ displayatonce: 3, navbuttons: $('div#nav').find('a') }); }); read more javascript self executing anonymous function here
javascript self executing function "is not function"
or
http://markdalgleish.com/2011/03/self-executing-anonymous-functions/
Comments
Post a Comment