Chris
Chris

Reputation: 461

Javascript Closure Keeps Returning a Function But Should Be a String

I have written the following closure that should return a string but it returns a function object. What am I doing wrong here?

BDS.CDNS = (function() {

var DEVROOT;
var TESTROOT = '/';
var PRODROOT = '/';
var _IsSecure;

return {
    CDN1: function CDN1() {
        if (BDS.ENV === BDS.ENV_OPTIONS.DEV) {
            return (_IsSecure ? BDS.SECUREPROTOCOL : BDS.UNSECUREPROTOCOL) + DEVROOT;
        }
        else if (BDS.ENV === BDS.ENV_OPTIONS.TEST) {
            return (_IsSecure ? BDS.SECUREPROTOCOL : BDS.UNSECUREPROTOCOL) + TESTROOT;
        }
        else if (BDS.ENV === BDS.ENV_OPTIONS.PROD) {
            return (_IsSecure ? BDS.SECUREPROTOCOL : BDS.UNSECUREPROTOCOL) + PRODROOT;
        }
        return '';
    }
}());

When calling BDS.CDNS.CDN1 => function object. It should return a string.

Thanks.

Upvotes: 0

Views: 115

Answers (1)

Kenneth
Kenneth

Reputation: 28737

When you reference BDS.CDNS.CDN1, you're getting a function object, because it is a function. In order to execute this function you need to include parenthesis:

BDS.CDNS.CDN1();

Upvotes: 3

Related Questions