Jeffrey L. Roberts
Jeffrey L. Roberts

Reputation: 2984

How to execute nested Javascript function

I am troubleshooting a slider problem at the moment, however, I don't know javascript that well, I have isolated the .js file that is responsible for the slider functioning, there is a destroy function that I would like to fire off, the code looks like this

(function ($) {

$.pixelentity = $.pixelentity || {version: '1.0.0'};

$.pixelentity.peBackgroundSlider = {    
    conf: {
        api: false,
        wait: false
    },
    paused: false
};

function PeBackgroundSlider(target, conf) {

    ...

    function destroy() {
        prevColor = currentColor = currentBW = jwindow = jthis = undefined;
        target.data("peBackgroundSlider", null);
        target = undefined;
    }
}

How would I fire off the destroy function in this scenario?

Upvotes: 0

Views: 39

Answers (1)

pid
pid

Reputation: 11597

You can't as it is right now.

To call it you must "export" it as follows:

function PeBackgroundSlider(target, conf) {

...

function destroy() {
    prevColor = currentColor = currentBW = jwindow = jthis = undefined;
    target.data("peBackgroundSlider", null);
    target = undefined;
  }

  return { "destroy": destroy };
}

From the caller:

var ret = PeBackgroundSlider();

Now you can do:

ret.destroy();

Or, more concise:

  return destroy;

And:

innerDestroy = PeBackgroundSlider();

And finally:

innerDestroy();

Upvotes: 2

Related Questions