How to create a constructor for a static library

I am using introjs library.

See the source code here .

I want to write var = new IntroJs() instead of calling the start() method.

How can i achieve this?

+4
source share
4 answers

Ok, so basically this solved my problem:

 introJs.fn = IntroJs.prototype = { ... initialize: function() { return this; } ... } 

Now calling introJs().initialize() gives me the library without calling the start() method.

0
source

Why not just wrap the factory that introJs provides and run it in your shell?

You can do it externally with something like this (untested):

 var introJsWrapper = function(targetElm) { var ijs = introJs(targetElm); ijs.start(); return ijs; }; 

Or you can do this inside the fork of introJs code by exposing it as a property of the main function, for example:

 var introJs = function (targetElm) { if (typeof (targetElm) === 'object') { // ... } introJs.autoStart = function(targetElm) { var ijs = introJs(targetElm); ijs.start(); return ijs; }; 

Note that in introJs the main function is a very subtle parameter check / shell replacement already around the internal constructor. The call indirectly calls the constructor. Therefore, I really do not need to directly access this internal design function, as far as I can tell.

+2
source

Well, that should be so. I assume they are enclosed in closure, as the code seems to imply that there are some internal functions. Here is what I put together. This is not a complete implementation, since I do not know how this used in the constructor when calling new IntroJS . All I know is that your prototype functions work with some properties.

 //internal functions function _mergeOptions(target){/*implementation*/} function _introForElement(el){/*implementation*/} function _goToStep(step){/*implementation*/} function _exitIntro(target){/*implementation*/} function _setHelperLayerPosition(nodeList){/*implementation*/} //constructor function IntroJs(first){ this._options = {}; this._introChangeCallback; this._introCompleteCallback; this._introExitCallback; } 
+1
source

Just an empty constructor will be enough. As Jan said, it's pretty useless, but if you like the notation ...

http://plnkr.co/edit/eFzkKJ14TeaMY44GDxR2

0
source

Source: https://habr.com/ru/post/1479984/


All Articles