Q:

Object literals concatenate string attributes

I'm using Javascript Object literals , but i can't concatenate string attributes.
var cart  = {
baseURL   : "http://www.domain.com/",
addURL    : this.baseURL + "cart/add",
deleteURL : this.baseURL + "cart/delete",
totalURL  : this.baseURL + "cart/total",
// functions
}// cart

i get a link as http://www.domain.com/undefinedcart/add
Please any help,
Thanks in advance

A:

You don't have access to the baseURL in that way. Thats because this is actually window and it probably does not has property baseURL.
You can use Immediately-Invoked Function Expression (IIFE) and closure instead:
var cart = function () {
   var baseURL = "http://www.domain.com/";
   return {
      addURL    : baseURL + "cart/add",
      deleteURL : baseURL + "cart/delete",
      totalURL  : baseURL + "cart/total"
   };
}();

