[英]Calling JavaScript sibling function inside parent function
[英]Calling a parent function inside a closure
我在嘗試在異步soap請求中調用對象函數時遇到了非常非常困難的事情。 基本上可以歸結為:
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);
我已經嘗試了很多事情,但是我認為從根本上可以歸結為以下事實:我無法在任何異步肥皂函數內調用this.foo()
。 盡管我可以將回調函數傳遞給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使函數作為對象,但是我有點沮喪。
我已經嘗試了很多很多事情,並且找不到解決方法,並且需要這樣做,因為對foo
的原始調用實際上是我在state-itterator模式中用於在SOAP Web中進行身份驗證的遞歸函數我用Java編寫的服務。
我想到的最后一種方法是修改SOAP npm包,以便可以將this.cookies
傳遞給createClient
而不只是回調。
我真的全都沒主意了。 任何幫助將不勝感激。
原因是在回調函數中, this
通常會引用全局窗口范圍。
this
是JS開發人員普遍的痛苦之源,而且通常不是指您認為的那樣。 您可以在線搜索全部詳細信息。
為什么不這樣封閉呢?
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();
});
});
}
}
A.將嵌套函數聲明為命名函數,或在閉包內將匿名函數分配給命名變量,允許嵌套函數由閉包內的任何其他函數調用, 而無需使用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
值的變量,允許閉包內的函數使用變量名訪問其實例對象。 這與菲利普的答案中包含的技術相同:
`var self = this;`
C.使用分配給命名變量的匿名箭頭函數 。 可以使用變量名從閉包中的其他嵌套函數調用它們。 箭頭函數從聲明它們的時間和位置綁定this
值。 因此,箭頭函數可以使用this
方法訪問其Thing
對象實例,而無需單獨的self
變量別名。 例如
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
參數(作為構造函數調用)可以在閉包內訪問,只需使用參數名稱即可。 為了從請求cookie對象設置名稱/值對對象,您可能需要一個沒有this
的對象初始化程序
args = {
foo: request.cookies.foo // request is a parameter
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.