简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM