简体   繁体   English

从对象文字模式返回值

[英]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. 否则,它们只是提供对功能对象本身的访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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