Javascript 多个 "try" 秒
Javascript multiple "try"s
我们都知道JS中的基本转义机制:
try {
...
}
catch(err) {
..
}
我有一个 JSON 数据,我想在其中检查潜在客户是否有全名。如果没有,尝试用名字和姓氏字段组成一个(我实际上在那里硬编码 space 这也是一个问题。)最后如果全部失败显示 "No Name".
或者伪代码:
try {
name = lead['Details']['Name']['Full'];
} else try {
name = lead['Details']['Name']['First'] + " " + lead['Details']['Name']['Last'];
} catch (e) {
name = "No Name";
}
有什么建议吗?
您可以将第二个 try/catch 块嵌套在第一个 try/catch 块的异常块中,如下所示:
try {
name = lead['Details']['Name']['Full'];
} catch(ex0) {
try {
name = lead['Details']['Name']['First'] + " " + lead['Details']['Name']['Last'];
} catch (ex1) {
name = "No Name";
}
}
这是有效的语法,在功能上等同于您所需要的
除了 2 年后的事实,但在 Babeljs 的支持下,最合适的解决方案应该基于 Optional chaining,这个 Q 仍然是练习完全基于函数的方法的完美目标。
实施例如afterThrowing
method modifier 可能导致表达式与 OP 的伪代码一样短且接近...
try {
name = lead['Meta']['Name']['Full'];
} else try {
name = lead['Details']['Name']['First'] + " " + lead['Details']['Name']['Last'];
} catch (e) {
name = 'No Name';
}
...然后会变成...
function getFullName(lead) {
return lead['Meta']['Name']['Full'];
}
function getComposedFullName(lead) {
return lead['Details']['Name']['First'] + " " + lead['Details']['Name']['Last'];
}
const name = (getFullName.afterThrowing((error, [data] = args) =>
(getComposedFullName.afterThrowing((/*error,[data]=args*/) => 'No Name')(data))
)(lead));
...实现和示例代码作为概念证明...
const leadTestSample = {
success: {
fullName: {
Meta: {
Name: {
Full: "Mary Jane Doe"
}
}
},
composed: {
Details: {
Name: {
First: "Jane",
Last: "Doe"
}
}
}
},
failure: {
Meta: {},
Details: {}
}
};
function getFullName(lead) {
return lead['Meta']['Name']['Full'];
}
function getComposedFullName(lead) {
return lead['Details']['Name']['First'] + " " + lead['Details']['Name']['Last'];
}
const nameDefault = 'No Name';
/*
try {
name = lead['Meta']['Name']['Full'];
} else try {
name = lead['Details']['Name']['First'] + " " + lead['Details']['Name']['Last'];
} catch (e) {
name = 'No Name';
}
*/
const fullNameData = leadTestSample.success.fullName;
const composedData = leadTestSample.success.composed;
const failureData = leadTestSample.failure;
console.log(
(getFullName.afterThrowing((error, [data] = args) =>
(getComposedFullName.afterThrowing(() => nameDefault)(data))
)(fullNameData))
);
console.log(
(getFullName.afterThrowing((error, [data] = args) =>
(getComposedFullName.afterThrowing(() => nameDefault)(data))
)(composedData))
);
console.log(
(getFullName.afterThrowing((error, [data] = args) =>
(getComposedFullName.afterThrowing(() => nameDefault)(data))
)(failureData))
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
<script>
(function (Function) {
const fctPrototype = Function.prototype;
const FUNCTION_TYPE = (typeof Function);
function isFunction(type) {
return (
(typeof type == FUNCTION_TYPE)
&& (typeof type.call == FUNCTION_TYPE)
&& (typeof type.apply == FUNCTION_TYPE)
);
}
function getSanitizedTarget(target) {
return ((target != null) && target) || null;
}
function afterThrowing/*Modifier*/(handler, target) {
target = getSanitizedTarget(target);
const proceed = this;
return (
isFunction(handler) &&
isFunction(proceed) &&
function () {
const context = target || getSanitizedTarget(this);
const args = arguments;
let result;
try {
result = proceed.apply(context, args);
} catch (exception) {
result = handler.call(context, exception, args);
}
return result;
}
) || proceed;
}
// afterThrowing.toString = () => 'afterThrowing() { [native code] }';
Object.defineProperty(fctPrototype, 'afterThrowing', {
configurable: true,
writable: true,
value: afterThrowing/*Modifier*/
});
}(Function));
</script>
我不介意有一天,JavaScript 正式推出...... Function.prototype[
before
|
after
|
around
|
afterThrowing
|
afterFinally
]
.
而不是仅使用一个 try-catch 块来执行此操作。正如我们所知,当访问不存在的内容时,我们什么时候会在 javascript 中出现类型错误。
See the below code snippet.
function getName(isFullName='true'){
try{
if(isFullName)
name = lead['Details']['Name']['Full'];
else
name = lead['Details']['Name']['First'] + " " + lead['Details']['Name']['Last'];
}
catch(e){
if (e instanceof TypeError && isFullName)
getName(false);
name = 'No name';
}
}
如果名称是全名未定义,那么我们需要添加一个额外的 if 块来检查未定义的大小写。
我们都知道JS中的基本转义机制:
try {
...
}
catch(err) {
..
}
我有一个 JSON 数据,我想在其中检查潜在客户是否有全名。如果没有,尝试用名字和姓氏字段组成一个(我实际上在那里硬编码 space 这也是一个问题。)最后如果全部失败显示 "No Name".
或者伪代码:
try {
name = lead['Details']['Name']['Full'];
} else try {
name = lead['Details']['Name']['First'] + " " + lead['Details']['Name']['Last'];
} catch (e) {
name = "No Name";
}
有什么建议吗?
您可以将第二个 try/catch 块嵌套在第一个 try/catch 块的异常块中,如下所示:
try {
name = lead['Details']['Name']['Full'];
} catch(ex0) {
try {
name = lead['Details']['Name']['First'] + " " + lead['Details']['Name']['Last'];
} catch (ex1) {
name = "No Name";
}
}
这是有效的语法,在功能上等同于您所需要的
除了 2 年后的事实,但在 Babeljs 的支持下,最合适的解决方案应该基于 Optional chaining,这个 Q 仍然是练习完全基于函数的方法的完美目标。
实施例如afterThrowing
method modifier 可能导致表达式与 OP 的伪代码一样短且接近...
try {
name = lead['Meta']['Name']['Full'];
} else try {
name = lead['Details']['Name']['First'] + " " + lead['Details']['Name']['Last'];
} catch (e) {
name = 'No Name';
}
...然后会变成...
function getFullName(lead) {
return lead['Meta']['Name']['Full'];
}
function getComposedFullName(lead) {
return lead['Details']['Name']['First'] + " " + lead['Details']['Name']['Last'];
}
const name = (getFullName.afterThrowing((error, [data] = args) =>
(getComposedFullName.afterThrowing((/*error,[data]=args*/) => 'No Name')(data))
)(lead));
...实现和示例代码作为概念证明...
const leadTestSample = {
success: {
fullName: {
Meta: {
Name: {
Full: "Mary Jane Doe"
}
}
},
composed: {
Details: {
Name: {
First: "Jane",
Last: "Doe"
}
}
}
},
failure: {
Meta: {},
Details: {}
}
};
function getFullName(lead) {
return lead['Meta']['Name']['Full'];
}
function getComposedFullName(lead) {
return lead['Details']['Name']['First'] + " " + lead['Details']['Name']['Last'];
}
const nameDefault = 'No Name';
/*
try {
name = lead['Meta']['Name']['Full'];
} else try {
name = lead['Details']['Name']['First'] + " " + lead['Details']['Name']['Last'];
} catch (e) {
name = 'No Name';
}
*/
const fullNameData = leadTestSample.success.fullName;
const composedData = leadTestSample.success.composed;
const failureData = leadTestSample.failure;
console.log(
(getFullName.afterThrowing((error, [data] = args) =>
(getComposedFullName.afterThrowing(() => nameDefault)(data))
)(fullNameData))
);
console.log(
(getFullName.afterThrowing((error, [data] = args) =>
(getComposedFullName.afterThrowing(() => nameDefault)(data))
)(composedData))
);
console.log(
(getFullName.afterThrowing((error, [data] = args) =>
(getComposedFullName.afterThrowing(() => nameDefault)(data))
)(failureData))
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
<script>
(function (Function) {
const fctPrototype = Function.prototype;
const FUNCTION_TYPE = (typeof Function);
function isFunction(type) {
return (
(typeof type == FUNCTION_TYPE)
&& (typeof type.call == FUNCTION_TYPE)
&& (typeof type.apply == FUNCTION_TYPE)
);
}
function getSanitizedTarget(target) {
return ((target != null) && target) || null;
}
function afterThrowing/*Modifier*/(handler, target) {
target = getSanitizedTarget(target);
const proceed = this;
return (
isFunction(handler) &&
isFunction(proceed) &&
function () {
const context = target || getSanitizedTarget(this);
const args = arguments;
let result;
try {
result = proceed.apply(context, args);
} catch (exception) {
result = handler.call(context, exception, args);
}
return result;
}
) || proceed;
}
// afterThrowing.toString = () => 'afterThrowing() { [native code] }';
Object.defineProperty(fctPrototype, 'afterThrowing', {
configurable: true,
writable: true,
value: afterThrowing/*Modifier*/
});
}(Function));
</script>
我不介意有一天,JavaScript 正式推出...... Function.prototype[
before
|
after
|
around
|
afterThrowing
|
afterFinally
]
.
而不是仅使用一个 try-catch 块来执行此操作。正如我们所知,当访问不存在的内容时,我们什么时候会在 javascript 中出现类型错误。
See the below code snippet.
function getName(isFullName='true'){
try{
if(isFullName)
name = lead['Details']['Name']['Full'];
else
name = lead['Details']['Name']['First'] + " " + lead['Details']['Name']['Last'];
}
catch(e){
if (e instanceof TypeError && isFullName)
getName(false);
name = 'No name';
}
}
如果名称是全名未定义,那么我们需要添加一个额外的 if 块来检查未定义的大小写。