使用命名参数 JavaScript(基于打字稿)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42108807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using named parameters JavaScript (based on typescript)
提问by user7425470
I have problems, when using named parameter in TypeScript, I know it is not supportetd the way I use it in TS. But how can I
我有问题,在 TypeScript 中使用命名参数时,我知道它不支持我在 TS 中使用它的方式。但是我怎么能
TypeScript:
打字稿:
SomeFunction(name1: boolean, name2: boolean, name3: boolean, name4: boolean) //will occur only 1 time, so the change should be in typescript
JavaScript:
JavaScript:
$(function () {
...SomeFunction({name1:false, name2:false, name3:false, name4:true}); //will occur 100 times
});
I was looking at(this did not work out):
我在看(这没有用):
Named parameters in javascript
How can I add optional named parameters to a TypeScript function parameter?
如何将可选的命名参数添加到 TypeScript 函数参数?
What can I do in TypeScript, to use named parameters in JavaScript?
我可以在 TypeScript 中做什么才能在 JavaScript 中使用命名参数?
What I wonder is, that VS2015 did not show a syntax error when using named parameter the way I used it in TypeScript ...
我想知道的是,VS2015 在使用命名参数时没有显示语法错误,就像我在 TypeScript 中使用它的方式一样......
ps.: I use TS 2.1
ps.:我使用 TS 2.1
回答by Paleo
You can use named parameters:
您可以使用命名参数:
interface Names {
name1: boolean
name2: boolean
name3: boolean
name4: boolean
}
function myFunction({name1, name2, name3, name4}: Names) {
// name1, etc. are boolean
}
Notice: The type Namesis actually optional. The following JavaScript code (without typing) is valid in TS:
注意:类型Names实际上是可选的。以下 JavaScript 代码(无需输入)在 TS 中有效:
function myFunction({name1, name2, name3, name4}) {
// name1, etc. are of type any
}
A precision: in reality, this feature is destructuringused to simulate named parameters. True named parameters don't exist in JavaScript nor in TypeScript.
一个精度:实际上,这个特性是用来模拟命名参数的解构。JavaScript 和 TypeScript 中都不存在真正的命名参数。
回答by Nitzan Tomer
The only to get something close to "named parameters" is to use a single object parameter:
获得接近“命名参数”的唯一方法是使用单个对象参数:
type SomeFunctionParams = {
name1: boolean;
name2: boolean;
name3: boolean;
name4: boolean;
}
SomeFunction(params: SomeFunctionParams) { ... }
And then:
进而:
$(function () {
SomeFunction({
name1:false,
name2:false,
name3:false,
name4:true
});
});
回答by Kieran Ryan
Simple solution would be to use meaningfully named local variables:
简单的解决方案是使用有意义命名的局部变量:
var mungo: boolean=true, mary: boolean=true, and: boolean=true, midge: boolean=true;
zapOn(mungo, mary, and, midge);
People: this is only a first step simple solution.. following the principles of test driven development: test fails, get it working - test green (this recommendation), refactor (if required, put the locals in an object and pass that to the function)
人:这只是第一步简单的解决方案......遵循测试驱动开发的原则:测试失败,让它工作 - 测试绿色(这个建议),重构(如果需要,将本地人放在一个对象中并将其传递给功能)
If you define the zapOn function in Typescript so: zapOn({mungo, mary=false, midge , and}: TVInterface) {...} where TVInterface {mary?: boolean, mungo: boolean, and: boolean, midge: boolean} and pass the tv object = {mary: undefined, mungo: mungo, and: and, midge: midge} ie zapOn(tv) thanks to object destructuring at function params level the variables/params in the function will correctly line up.. in the case of the ?mary“ parameter we also set a default value of false
如果你在 Typescript 中定义 zapOn 函数,那么: zapOn({mungo, mary=false, midge , and}: TVInterface) {...} where TVInterface {mary?: boolean, mungo: boolean, and: boolean, midge: boolean并传递 tv 对象 = {mary: undefined, mungo: mungo, and: and, midge: midge} 即 zapOn(tv) 由于在函数参数级别的对象解构,函数中的变量/参数将正确排列..在 ?mary“ 参数的情况下,我们还将默认值设置为 false

