Steve Medley Steve Medley - 1 year ago 67
Javascript Question

Calling a variable stored in a literal notation object

I have this literal notation object (took out the irrelevant parts):

var work = {
"display": function displayWork(){
for(i in{

var formattedEmployer = HTMLworkEmployer.replace('%data%',[i].employer);
var formattedTitle = HTMLworkTitle.replace('%data%',[i].title);
var formattedEmployerTitle = formattedEmployer + formattedTitle;
var formattedWorkDates = HTMLworkDates.replace('%data%',[i].dates);
var formattedWorkLocation = HTMLworkLocation.replace('%data%',[i].location);
var formattedWorkDescription = HTMLworkDescription.replace('%data%',[i].description);


I have tried calling it with:


None of these have worked. How do I go about calling this function?

If i place the function outside of the literal notation object than I can call it just fine with:


Answer Source

Like this:


The work object's display property refers to the function.

Because your function has been created with a function expression, the name displayWork is only usable from within the function, so if the function doesn't need to call itself (recursively) you could choose to omit this name and just say display : function() { ... }.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download