GibboK
GibboK

Reputation: 73908

How to get a property when setting others in Object Literal notation?

In java script I get this error

Uncaught ReferenceError: baseUrl is not defined 



window.Configurations = Configurations = {
        baseUrl: 'https://mysite.com/',
        detailsEventCustom: baseUrl + 'DetailsEventCustom?EventId=',
        addEventCustom: baseUrl + 'AddEventCustom',
        listAllEventsCustomForDate: baseUrl + 'ListAllEventsCustomForDate?DateToLookUp=',
        dashboardEventsCustom: baseUrl + 'DashboardEventsCustom',
        listAllTimetableEventsCustom: baseUrl + 'ListAllTimetableEventsCustom',
        updateEventCustom: baseUrl + 'UpdateEventCustom?EventId=',
        deleteEventCustom: baseUrl + 'DeleteEventCustom?EventId='
    };

Could you point me out what I am doing wrong here?

Upvotes: 0

Views: 46

Answers (2)

rtytgat
rtytgat

Reputation: 497

This is a scoping problem. You are still building the object between the curly braces, and all values are calculated before they are assigned to the object. baseUrl simply doesn't exist yet when you are using it to assign the other values. You should do something like this instead:

var baseUrl = 'https://mysite.com/'
window.Configurations = Configurations = {
    baseUrl: baseUrl,
    detailsEventCustom: baseUrl + 'DetailsEventCustom?EventId=',
    addEventCustom: baseUrl + 'AddEventCustom',
    listAllEventsCustomForDate: baseUrl + 'ListAllEventsCustomForDate?DateToLookUp=',
    dashboardEventsCustom: baseUrl + 'DashboardEventsCustom',
    listAllTimetableEventsCustom: baseUrl + 'ListAllTimetableEventsCustom',
    updateEventCustom: baseUrl + 'UpdateEventCustom?EventId=',
    deleteEventCustom: baseUrl + 'DeleteEventCustom?EventId='
};

Upvotes: 0

Parv Sharma
Parv Sharma

Reputation: 12705

you cannot do it like this
when you are accessing the object hasn't been made try doing this instead

var baseUrl = 'https://mysite.com/';
window.Configurations = Configurations = {
        baseUrl: baseUrl,
        detailsEventCustom: baseUrl + 'DetailsEventCustom?EventId=',
        addEventCustom: baseUrl + 'AddEventCustom',
        listAllEventsCustomForDate: baseUrl + 'ListAllEventsCustomForDate?DateToLookUp=',
        dashboardEventsCustom: baseUrl + 'DashboardEventsCustom',
        listAllTimetableEventsCustom: baseUrl + 'ListAllTimetableEventsCustom',
        updateEventCustom: baseUrl + 'UpdateEventCustom?EventId=',
        deleteEventCustom: baseUrl + 'DeleteEventCustom?EventId='
    };

Upvotes: 2

Related Questions