最简单的方法是你打出来的任何函数调用的同样的方式:抛出一个错误。
var obj = {
someCondition: false,
a: function() {
return this;
},
b: function() {
if (!this.someCondition) {
var error = new Error("A message");
error.name = "SomeConditionError";
throw error;
}
return this;
},
c: function() {
return this;
}
};
然后调用方法并处理错误。
try {
obj
.a()
.b() // throws a "SomeConditionError" here
.c();
}
catch (error) {
if (error.name == "SomeConditionError") {
// gracefully handle "SomeConditionError"
}
else {
// rethrow errors you don't know how to handle gracefully
throw error;
}
}
你想避免的一件事是using exceptions for flow control。
如果您需要致电obj.a()
然后obj.b()
,但后来有条件地obj.c()
然后调用代码需要处理:
obj.a().b();
if (someCondition) {
// Assign to "obj" just in case obj.c() returns a different object
obj = obj.c();
}
感觉就像丑陋的代码(和它有点),但这种传达在这些方法调用中引发的任何错误都是灾难性的,显示停止错误。如果你有一个复杂的操作,涉及多个方法调用一个物体或多个物体上,考虑封装在一个“command”:
function DoSomethingCommand(obj) {
this.obj = obj;
}
DoSomethingCommand.prototype = {
constructor: DoSomethingCommand,
execute: function() {
this.obj.a().b();
if (someCondition) {
this.obj = this.obj.c();
}
}
};
从调用代码的角度来看,它只是一个简单的调用到开始真正复杂的过程:
var command = new DoSomethingCommand(obj);
command.execute();
如果你写'c()',它将被执行。你可以做一个事情是取代B'的'的返回值'{C:函数(){}}'但是这是令人厌恶的。 –
@OmarElawady会引发错误 – Mark
如果您启动了它,则无法退出该功能。该函数在return语句或其主体结尾处结束。 –