在闭包中调用父函数

我有一个非常非常困难的时间试图在一个asynchronous肥皂请求中调用一个对象函数。 基本上归结为:

function Thing(request, response) { this.foo = function() { console.log("this is foo"); } this.doThing = function() { // Get SOAP args args = { foo: this.request.cookies.foo }; // From the SOAP npm package library soap.createClient(function(err, client) { client.doSomething(args, function(err, result) { // Somehow call foo(); <--- CAN'T FIND A WAY TO DO THIS }); }); } } // Make it so I can access this.request and this.response somehow Thing.prototype = Object.create(AbstractThing); 

我已经尝试了很多东西,但是我相信它从根本上归结为这样一个事实,即我不能在任何exception的soap函数中调用this.foo() 。 尽pipe我可以将一个callback函数传递给createClient ,如下所示:

 function Thing(request, response) { this.foo = function() { console.log("this is foo"); } this.sendSoap = function(err, client) { // Get SOAP args args = { foo: this.request.cookies.foo <--- this.cookies undefined }; client.doSomething(args, function(err, result) { // Somehow call foo(); <--- CAN'T FIND A WAY TO DO THIS }); } this.doThing = function() { // From the SOAP npm package library soap.createClient(this.sendSoap); } } // Make it so I can access this.request and this.response somehow Thing.prototype = Object.create(AbstractThing); 

我不能再访问this.request.cookies,因为this是在sendSoap闭包内调用的。 我不知道为什么JavaScript使function作为对象,但我有点沮丧。

我已经尝试了许多,很多东西,并不能找出一种方法来做到这一点,需要,因为原来的调用foo实际上是一个recursion函数,我正在使用状态行模式在SOAP Web中进行身份validation我用Java编写的服务。

我能想到的最后一种方法是修改SOAP npm包,以便this.cookies传递给createClient而不仅仅是callback。

我真的完全没有想法。 任何帮助将不胜感激。

thiscallback函数中的原因通常是指全局窗口范围。

this是JS开发人员常见的痛苦来源,更多的时候并不是指你认为的可能。 您可以在线search完整的详细信息。

为什么不这样closures呢?

 function Thing(request, response) { this.foo = function() { console.log("this is foo"); } this.doThing = function() { var self = this; //Closure to the Thing function // Get SOAP args args = { foo: this.request.cookies.foo }; // From the SOAP npm package library soap.createClient(function(err, client) { client.doSomething(args, function(err, result) { self.foo(); }); }); } } 

工作解决scheme的多个选项。

A.将嵌套函数声明为命名函数,或将匿名函数分配给闭包中的命名variables,允许嵌套函数由闭包内的任何其他函数调用, 而不使用this函数 例如

 // Either naming a function: function foo () { console.log("foo") // or assigning it to a named variable: var foo = function() { console.log("foo")}; 

允许函数在闭包内被调用为foo()

B.在Thing对象构造期间声明一个设置为this值的variables,允许闭包内的函数使用variables名访问它们的实例对象。 菲利普的答案中包含了同样的技巧:

 `var self = this;` 

C.使用分配给命名variables的匿名箭头函数 。 可以使用variables名称从闭包中的其他嵌套函数调用它们。 箭头函数从声明何时何地绑定this值。 因此,箭头函数可以使用它访问它们的Thing对象实例,而不需要单独的selfvariables别名。 例如

 var foo = () => {console.log(this.message)}; this.message = "foo was here"; foo(); // logs 'foo was here' 

D.当然,设置任何参数,嵌套函数或数据值必须作为对象属性在闭包之外访问。

 // inside constructor var foo = () => { /* whatever */ }; // callable within closure as foo() this.foo = foo; // outside constructor var thing = new Thing( arg1, arg2); thing.foo(); // callable outside closure as thing.foo() 

E)当被调用为构造函数时传递给Thing的参数只需使用参数名就可以在闭包中访问。 为了从一个请求cookies对象中设置一个名称值对对象,你可能需要一个没有this的对象初始化器

  args = { foo: request.cookies.foo // request is a parameter };