title: Feeding ServerSide data into JavaScript object constructor
body: <p>I am using ASP.NET MVC and I have a simple requirement. I need to be able to "inject" two server side values into my JavaScript object.</p>
  
  <p>My JavaScript object lives in a separate js file referenced by my project BUT I need to be able to inject data into my object prior to it being used for the first time.</p>
  
  <p>My code currently looks like this.</p>
  
  <pre><code>var ImageSizeEnum = {
      Small: 0,
      Medium: 1,
      Large: 2
  };
  
  var UrlBuilder = (function (baseImageUrl, storageContainer) {
  
      var _baseImageUrl = baseImageUrl;
      var _storageContainer = storageContainer;
  
      this.BuildImageUrl = function (stockImage, imageSizeEnum) {
              var imageSizeString = "";
              switch (imageSizeEnum) {
                  case ImageSizeEnum.Small:
                      imageSizeString = "sm";
                      break;
                  case ImageSizeEnum.Medium:
                      imageSizeString = "md";
                      break;
                  case ImageSizeEnum.Large:
                      imageSizeString = "lg";
                      break;
              }
  
              var url = kendo.format(_baseImageUrl + "_{2}{3}", _storageContainer, stockImage.AzureId, imageSizeString, stockImage.Extension);
              return url;
      };
  
  });
  </code></pre>
  
  <p>and it is invoked in my <code>_Layout.cshtml</code> page like this.</p>
  
  <pre><code>$(function () {
      UrlBuilder("@WebConfigSettings.BaseImageUrlPath", "@WebConfigSettings.AzureStorageContainer");
  });
  </code></pre>
  
  <p>But this code doesn't appear to work as I expected due to <code>BuildImageUrl</code> being undefined. I assume this is because I am not actually initializing and returning my UrlBuilder object by invoking it with () at declaration. </p>
  
  <p>I am somewhat aware of the different types of JavaScript object patterns (i.e. literals, function etc) but I am completely uncertain about what the 'correct' pattern and approach is to solve my particular problem. </p>
  
  <p>Any suggestions?</p>