最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

在对象内部的函数内部使用函数

IT培训 admin 4浏览 0评论

在对象内部的函数内部使用函数

我想将函数A调用为函数B。

我已经尝试从功能B调用功能A

module.exports = function(req, res) {
  return {
    function_a: function() {
      // do something here
    },
    function_b: function() {
      // call function_a
      const A = function_a(); // I want to be called something like this
      // do something here
    }
  }
}
回答如下:

假设function_b通过以下方式调用:

your_module().function_b();

然后您可以通过this中的function_b访问对象。

this.function_a();

如果该方法与对象分离,则将失去该连接,因此可能值得重写该模块,因此您获得的引用不依赖于this的值。例如:

module.exports = function(req, res) {

  const my_return_value = {
    function_a: function() {
      // do something here
    },
    function_b: function() {
      const A = my_return_value.function_a();
    }
  };

  return my_return_value;
}

在对象内部的函数内部使用函数

我想将函数A调用为函数B。

我已经尝试从功能B调用功能A

module.exports = function(req, res) {
  return {
    function_a: function() {
      // do something here
    },
    function_b: function() {
      // call function_a
      const A = function_a(); // I want to be called something like this
      // do something here
    }
  }
}
回答如下:

假设function_b通过以下方式调用:

your_module().function_b();

然后您可以通过this中的function_b访问对象。

this.function_a();

如果该方法与对象分离,则将失去该连接,因此可能值得重写该模块,因此您获得的引用不依赖于this的值。例如:

module.exports = function(req, res) {

  const my_return_value = {
    function_a: function() {
      // do something here
    },
    function_b: function() {
      const A = my_return_value.function_a();
    }
  };

  return my_return_value;
}
发布评论

评论列表 (0)

  1. 暂无评论