1

I am trying to do some logic inside of a JavaScript object to determine what the value should be, and am running into some difficulty.

I have a 'config' object:

var gRestfulServicesHost = new function() {
   return {
     hostUrlPrefix: 'http://',
     hostUrl: 'somehost.example.com',
     hostVitalSourceGPOUrl: 'local.somehost.com/restful',
     relativePathUI: '/application',
     relativePathRestfulSvc: function() {
      if(oGetVars.env) {
        switch (oGetVars.env) {
           case 'dev':
              console.warn('Using dev restful service');
              break;
           case 'qa':
              console.warn('Using qa restful service');
              break;
          case 'release':
             console.warn('Using release restful service');
             break;
          case 'staging':
             console.warn('Using staging restful service');
             break;
          case 'demo':
             console.warn('Using demo restufl service');
             break;
          default:
             console.warn('Using local restful service');
             break;
          }
       }
       return '/some-provider/api';
    }
  }
};

But then when I go to retrieve the values, something like this works just fine:

gRestfulServicesHost.hostUrlPrefix // inspector shows 'http://'

But when I want to use relativePathRestfulSvc, like this:

gRestfulServicesHost.relativePathRestfulSvc // inspector shows: function () { ... }

I want the return value of the function, but it's like the function isn't even being eval'd. I've even tried it with the parens gRestfulServicesHost.relativePathRestfulSvc() but it still doesn't work.

Thanks

0

2 Answers 2

2

That's because you're not actually calling the function, you're assigning a function to the relativePathRestfulSvc property. To actually run the code, you must do this:

relativePathRestfulSvc: (function() {
  if(oGetVars.env) {
    switch (oGetVars.env) {
       case 'dev':
          console.warn('Using dev restful service');
          break;
       case 'qa':
          console.warn('Using qa restful service');
          break;
      case 'release':
         console.warn('Using release restful service');
         break;
      case 'staging':
         console.warn('Using staging restful service');
         break;
      case 'demo':
         console.warn('Using demo restufl service');
         break;
      default:
         console.warn('Using local restful service');
         break;
      }
   }
   return '/some-provider/api';
})()

Notice that the whole function has been wrapped in parentheses (), and then is immediately called (the following ()), which will assign the return value of the function to the relativePathRestfulSvc property.

This pattern is known as "Immediately Invoked Function Expression" (IIFE, pronounced "iffy"), and generally takes the form of (function() { ... })(), where a function is declared and is immediately called/invoked.

You can read up about it here: http://en.wikipedia.org/wiki/Immediately-invoked_function_expression

Sign up to request clarification or add additional context in comments.

Comments

1

You're not actually calling the function anywhere. You'd want something like:

relativePathRestfulSvc: (function() {
      if(oGetVars.env) {
        switch (oGetVars.env) {
           case 'dev':
              console.warn('Using dev restful service');
              break;
           case 'qa':
              console.warn('Using qa restful service');
              break;
          case 'release':
             console.warn('Using release restful service');
             break;
          case 'staging':
             console.warn('Using staging restful service');
             break;
          case 'demo':
             console.warn('Using demo restufl service');
             break;
          default:
             console.warn('Using local restful service');
             break;
          }
       }
       return '/some-provider/api';
    })() // <--- Use () to call the function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.