我在for循环中使用它来分配变量,以便具有灵活的可配置解决方案。
以下代码:
defineRestApi (api, methodName, options = {}) {
for (const type of Object.keys (api)) {
for (const httpMethod of Object.keys (api[type])) {
let paths = api[type][httpMethod]
for (let i = 0; i < paths.length; i++) {
let path = paths[i].trim ()
let splitPath = path.split (/[^a-zA-Z0-9]/)
let uppercaseMethod = httpMethod.toUpperCase ()
let lowercaseMethod = httpMethod.toLowerCase ()
let camelcaseMethod = this.capitalize (lowercaseMethod)
let camelcaseSuffix = splitPath.map (this.capitalize).join ('')
let underscoreSuffix = splitPath.map (x => x.trim ().toLowerCase ()).filter (x => x.length > 0).join ('_')
let camelcase = type + camelcaseMethod + this.capitalize (camelcaseSuffix)
let underscore = type + '_' + lowercaseMethod + '_' + underscoreSuffix
if ('suffixes' in options) {
if ('camelcase' in options['suffixes'])
camelcase += options['suffixes']['camelcase']
if ('underscore' in options.suffixes)
underscore += options['suffixes']['underscore']
}
if ('underscore_suffix' in options)
underscore += options.underscoreSuffix;
if ('camelcase_suffix' in options)
camelcase += options.camelcaseSuffix;
let partial = async params => this[methodName] (path, type, uppercaseMethod, params || {})
this[camelcase] = partial
this[underscore] = partial
}
}
}
}
Typescript 2.7.2,NativeScript
let partial = async params => this[methodName] (path, type, uppercaseMethod, params || {})
给出错误
JS ERROR SyntaxError:意外的标识符&#39; params&#39;。期待&#39 ;;&#39;在变量声明之后。
tsc --version 2.7.2
tsconfig:
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"noEmitHelpers": true,
"noEmitOnError": true,
"lib": [
"es7",
"es6",
"dom",
"es2015.iterable"
],
"baseUrl": ".",
"paths": {
"*": [
"./node_modules/tns-core-modules/*",
"./node_modules/*"
]
}
},
"exclude": [
"node_modules",
"platforms",
"**/*.aot.ts"
]
}
看起来像async的函数表达式在某种程度上是错误的。