jonnyhitek
jonnyhitek

Reputation: 1561

Return value from object literal patern

Hi guys wondering if someone can help me, I am using the object literal pattern to organise my code (I'm new to this pattern). I am trying to return the value of a variable from a function but it keeps returning me the whole function - could someone tell me what I am doing wrong here is a snippet from my code -

    'teamStatusTableHeight': function() {
    var theHeight = $(".teamStatusTable").height() - 130;
    return theHeight;
},
'numOfTeamMembers': function() {
    var numTeams = $(".teamStatusTable tr").length;
    return numTeams
} ,
'scrollDistance': function() {
    var scroll = teamStatus.teamStatusTableHeight / teamStatus.numOfTeamMembers + 30;
    return scroll;
}

Any help would be greatly appreciated.

Upvotes: 0

Views: 3772

Answers (2)

Alex Wayne
Alex Wayne

Reputation: 187232

That looks fine to me. Perhaps you are not actually calling the function?

// get the function
MyObj.teamStatusTableHeight

// run the function
MyObj.teamStatusTableHeight()

Did you leave off the parentheses? They are required to actually execute the function. Otherwise they simply provide access to the function object itself.

Upvotes: 1

Nick Craver
Nick Craver

Reputation: 630589

You need to call those functions, like this:

var scroll = teamStatus.teamStatusTableHeight() / teamStatus.numOfTeamMembers() + 30;

Note the added () so you're using the result of the functions, not the functions themselves.

Upvotes: 2

Related Questions