Tuesday, 15 May 2012

javascript - jquery object - function undefined error -



javascript - jquery object - function undefined error -

in next function, objects within floatsharebar function undefined. have init or define var before functions? throws me js error : .float - function undefined.

(function($) { . . . $("body").on("ab.snap", function(event) { if (event.snappoint >= 768) { floatsharebar.float() } else { floatsharebar.unfloat(); } }); var floatsharebar = function() { var fsharebar = $('#article-share'); this.float = function() { console.log( }; this.unfloat = function() { console.log("unfloat"); }; }; . . . })(jquery);

you need instance of function self instantiating call:

var floatsharebar = (function() { var fsharebar = $('#article-share'); this.float = function() { console.log('float'); }; this.unfloat = function() { console.log("unfloat"); }; homecoming this; })();

update 1: modified create object within function attach functions to, since in previous illustration this refers window object

var floatsharebar = (function() { var fsharebar = $('#article-share'); var instance = {}; instance.float = function() { console.log('float'); }; instance.unfloat = function() { console.log("unfloat"); }; homecoming instance; })();

update 2: can utilize new keyword well, look here more info

var floatsharebar = new (function() { var fsharebar = $('#article-share'); this.float = function() { console.log('float'); }; this.unfloat = function() { console.log("unfloat"); }; })();

javascript jquery

No comments:

Post a Comment