Meteor 如何在当前函数中调用另一个函数?

Meteor how to call another function in current function?

我在助手中有一个功能。我想在下一个函数中调用该函数。我怎么能掉下来呢?

'first_func' ()
{
    return "hello";
},
'second_func' ()
{
    return this.first_func();
}

这是行不通的。我想在第二个函数中调用第一个函数。

谢谢你!

按照您尝试执行此操作的方式,您将不需要 this。所以你可以像这样调用第一个函数:

function first_func() {
   //`this` is bound to first_func()
   return "hello";
}

function second_func () {
   //`this` is bound to second_func()
   return first_func();
}

second_func(); // returns 'hello'

但是,您似乎正试图在 class 或方法中调用函数。不过,我无法猜测如何或为什么,所以请在 Can you write nested functions in JavaScript?

上查看答案

正如@thatgibbyguy 所说,您可以在同一个文件中定义一个函数并使用它。在 Meteor 模板助手中,this 是助手的 数据上下文 ,而不是模板实例本身。

Template.myTemplate.helpers(
  first_func(){
    return myFunction();
  },
  second_func(){
    return myFunction();
  }
);
function myFunction(){
   return "Hello"
}

您还可以 register a global helper 可以从任何模板使用