Javascript 如何始终在函数结束时运行清理?
假设我有这个函数(psuedo代码):Javascript 如何始终在函数结束时运行清理?,javascript,node.js,Javascript,Node.js,假设我有这个函数(psuedo代码): 来自C++和C世界,我只需在析构函数中实现清理,或者使用 Goto 。但是JavaScript也没有。 每当Foo()返回时,我将如何调用CleanUp() 是在我返回的每个中调用CleanUp()的唯一方法吗?在javascript中,您可以在函数(即闭包)中定义和调用函数 通过这种方式,您可以通过以下方式实现所需: function Foo() { let varThatRequiresCleanup = //something // De
来自C++和C世界,我只需在析构函数中实现清理,或者使用<代码> Goto 。但是JavaScript也没有。 每当
Foo()
返回时,我将如何调用CleanUp()
是在我返回的每个
中调用CleanUp()
的唯一方法吗?在javascript中,您可以在函数(即闭包)中定义和调用函数
通过这种方式,您可以通过以下方式实现所需:
function Foo() {
let varThatRequiresCleanup = //something
// Define your "actual" Foo logic in an inner function
// where you can define your flow and return logic as
// needed.
function InnerFoo() {
// You can access variables in the Foo closure, like so
console.log(varThatRequiresCleanup);
if(condition1) {
return Error1;
}
if(condition2) {
return Error2;
}
if(condition3) {
return Error3;
}
//etc.. more ifs and important code.
return Success;
}
// Now call your inner function like so
var result = InnerFoo();
// Now you can cleanup resourced in scope of Foo as
// in this way, because this line will be hit
varThatRequiresCleanup.CleanUp();
// Return the result from InnerFoo()
return result;
}
在javascript中,您可以在函数(即闭包)中定义和调用函数
通过这种方式,您可以通过以下方式实现所需:
function Foo() {
let varThatRequiresCleanup = //something
// Define your "actual" Foo logic in an inner function
// where you can define your flow and return logic as
// needed.
function InnerFoo() {
// You can access variables in the Foo closure, like so
console.log(varThatRequiresCleanup);
if(condition1) {
return Error1;
}
if(condition2) {
return Error2;
}
if(condition3) {
return Error3;
}
//etc.. more ifs and important code.
return Success;
}
// Now call your inner function like so
var result = InnerFoo();
// Now you can cleanup resourced in scope of Foo as
// in this way, because this line will be hit
varThatRequiresCleanup.CleanUp();
// Return the result from InnerFoo()
return result;
}
一种替代方法是对重复代码使用函数:
函数Foo(){
让varThatRequiresCleanup=//一些东西
函数清理和返回(返回值){
varThatRequiresCleanup.CleanUp();
返回值;
}
如果(条件1){返回清除和返回(错误1);}
如果(条件2){返回清除和返回(错误2);}
如果(条件3){返回清除和返回(错误3);}
//等等,更多的国际单项体育联合会和重要的代码。
返回清理和返回(成功);
}
一种替代方法是为重复代码使用函数:
函数Foo(){
让varThatRequiresCleanup=//一些东西
函数清理和返回(返回值){
varThatRequiresCleanup.CleanUp();
返回值;
}
如果(条件1){返回清除和返回(错误1);}
如果(条件2){返回清除和返回(错误2);}
如果(条件3){返回清除和返回(错误3);}
//等等,更多的国际单项体育联合会和重要的代码。
返回清理和返回(成功);
}
你可以做一些叫做猴子补丁的事情
概念取自
你可以做一些叫做猴子补丁的事情
概念取自
您可以使用try finally块:
function Foo() {
let varThatRequiresCleanup = //something
try {
if(condition1) {
return Error1;
}
if(condition2) {
return Error2;
}
if(condition3) {
return Error3;
}
//etc.. more ifs and important code.
return Success;
} finally {
varThatRequiresCleanup.CleanUp();
}
}
您可以使用try finally块:
function Foo() {
let varThatRequiresCleanup = //something
try {
if(condition1) {
return Error1;
}
if(condition2) {
return Error2;
}
if(condition3) {
return Error3;
}
//etc.. more ifs and important code.
return Success;
} finally {
varThatRequiresCleanup.CleanUp();
}
}
您可以使用代理拦截对方法的调用
它通过将新代理
返回给构造函数来工作。然后,我们处理get
调用,利用该调用,我们可以首先调用该方法,然后运行清理。这将允许我们只需在代理中调用cleanup一次,而不必在方法本身中调用
创建返回新代理的实例
调用其中一个方法
代理现在将拦截并处理该调用
代理将按原样运行函数
然后代理将运行清理功能
然后,代理将返回步骤2的结果
class-MyClass{
构造函数(){
this.varThatRequiresCleanup=null
//创建对“this”的引用,以便下面的函数可以使用它
让$this=这个
//一个私人的功能,这将清理东西
//我们无法在此函数中使用'this',因为它引用了窗口
//我们将使用我们的引用“$this”
函数清理(){
$this.varThatRequiresCleanup=null
console.log('已清理')
}
//创建将处理方法调用的代理
返回新代理(此{
get:(目标、道具、接受者)=>{
//获取被调用的属性
让targetValue=Reflect.get(目标、道具、接收器)
//执行被调用的属性
返回(…参数)=>{
设r=targetValue.apply(此参数为args)
console.log('在varThatRequiresCleanup之前:',this.varThatRequiresCleanup)
清理()
console.log('在varThatRequiresCleanup:'之后,this.varThatRequiresCleanup)
返回r
}
}
})
}
Foo(){
this.varThatRequiresCleanup='foo'
返回“foo结果”
}
Bar(){
this.varThatRequiresCleanup='bar'
返回“条结果”
}
}
设c=newmyclass()
log('Foo返回值:',c.Foo())
console.log('------------')
console.log('Bar返回值:',c.Bar())
您可以使用代理来拦截对方法的调用
它通过将新代理
返回给构造函数来工作。然后,我们处理get
调用,利用该调用,我们可以首先调用该方法,然后运行清理。这将允许我们只需在代理中调用cleanup一次,而不必在方法本身中调用
创建返回新代理的实例
调用其中一个方法
代理现在将拦截并处理该调用
代理将按原样运行函数
然后代理将运行清理功能
然后,代理将返回步骤2的结果
class-MyClass{
构造函数(){
this.varThatRequiresCleanup=null
//创建对“this”的引用,以便下面的函数可以使用它
让$this=这个
//一个私人的功能,这将清理东西
//我们无法在此函数中使用'this',因为它引用了窗口
//我们将使用我们的引用“$this”
函数清理(){
$this.varThatRequiresCleanup=null
console.log('已清理')
}
//创建一个代理,该代理将处理方法调用
返回新代理(此{
get:(目标、道具、接受者)=>{
//获取被调用的属性
让targetValue=Reflect.get(目标、道具、接收器)
//执行被调用的属性
返回(…参数)=>{
设r=targetValue.apply(此参数为args)
console.log('在varThatRequiresCleanup之前:',this.varThatRequiresCleanup)
清理()
console.log('在varThatRequiresCleanup:'之后,this.varThatRequiresCleanup)
返回r
}
}
})
}
Foo(){
this.varThatRequiresCleanup='foo'
返回“foo结果”
}
Bar(){
this.varThatRequiresCleanup='bar'
返回“条结果”
}
}
设c=newmyclass()
log('Foo返回值:',c.Foo())
console.log('------------')
console.log('Bar返回值