javascript - Factory unable to create objects defined within an IIFE object -
i'm creating gallery transitions slides based on various transition objects:
window.mygallery = (function () { var instance = null; .... //base transition function transition(slide, settings){ this.slide = slide; this.el = slide.el; this.settings = settings; this.duration = (this.settings['transitionspeed'] / 1000) + 's'; this.endanimation = null; } //fade transition function fadetransition(slide, settings){ transition.call(this, slide, settings); } fadetransition.prototype = object.create(transition.prototype); fadetransition.prototype.constructor = fadetransition; //slide transition function slidetransition(slide, settings){ transition.call(this, slide, settings); } slidetransition.prototype = object.create(transition.prototype); slidetransition.prototype.constructor = slidetransition;
to appropriate transition object, built factory:
var transitionfactory = function () { return { createtransition: function (slide, transitiontype, settings) { if (transitiontype) { type = transitiontype; } if (type == 'fade') { return new fadetransition(slide, settings); } else if(type == 'slide'){ return new slidetransition(slide, settings); } }, //set default transition type: "fade" }; };
since envision dozens of different transitions, thought clever , take in string create transition object dynamically:
createtransition: function (slide, transitiontype, settings) { if (transitiontype) { type = transitiontype; } type = type.charat(0).touppercase() + type.slice(1); type += 'transition'; if(typeof window[type] != 'undefined') //boom return new window[type](slide, settings); else return new fadetransition(slide, settings); },
the issue fadetransition
defined within iife assigned window.mygallery
, since don't want pollute global scope objects. therefore, undefined
:
if(typeof window[type] != 'undefined')
how can test existence of object within iife scope , instantiate if exist?
i not sure if inner functions can accessed in iife, if it, have added functions in object, based on user's input have picked function object. here sample implementation.
var mygallery = (function() { var instance = null; //note: base transition function goes here function inheriter(child, parent) { child.prototype = object.create((parent || transition).prototype); child.prototype.constructor = child; } var transitions = { fadetransition: function(slide, settings) { transition.call(this, slide, settings); }, slidetransition: function(slide, settings) { transition.call(this, slide, settings); } }; (var transition in transitions) { inheriter(transitions[transition], transition); } return { createtransition: function(slide, transitiontype, settings) { type = (transitiontype || "fade") + "transition"; type = type.charat(0).touppercase() + type.slice(1); if (!transitions[type]) { type = "fadetransition"; } return new transitions[type](slide, settings); } }; })();
Comments
Post a Comment