How to call function with Universal Module Definition (UMD)

How to call function with Universal Module Definition (UMD)

  

In a mobile espace, Added the below javascript to a script file Script1.js to a mobile module. How to call the function testfn() on button click ?

Tried accessing with window.testfn(); with no success.


(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define([], factory);
    } else if (typeof exports === 'object') {
        // Node. Does not work with strict CommonJS, but
        // only CommonJS-like environments that support module.exports,
        // like Node.
        module.exports = factory();
    } else {
        // Browser globals (root is window)
        root.memoize = factory();
    }
}(this, function() {
    "use strict";

    var testfn = function(func) {
      alert('from testfn function');
    };

    return testfn;
}));





Hi,

I managed to successfully call the function like this:

Aurelio Junior wrote:

Hi,

I managed to successfully call the function like this:

Strange ! When I tried it throws an error.


Solution

You have to call it like this: window.memoize()

Not like this: window.testfn()

Solution

Aurelio Junior wrote:

You have to call it like this: window.memoize()

Not like this: window.testfn()

Yes it works. There were typo in my javascript.