gpt4 book ai didi

javascript - 使用: and => for the return type with a TypeScript function?有什么区别

转载 作者:数据小太阳 更新时间:2023-10-29 04:44:32 25 4
gpt4 key购买 nike

我有以下代码:

///<reference path="../typescript/jquery.d.ts" />
function addThemePrototypes() {
var templateSetup = new Array();
$.fn.addTemplateSetup = function(func, prioritary)
{
if (prioritary)
{
templateSetup.unshift(func);
}
else
{
templateSetup.push(func);
}
};
}

有人能告诉我为什么要用 => void 来声明吗?

interface JQuery {
addTemplateSetup: (func: Function, priority: bool) =>void;
}

我想我对如何从 javascript 函数执行 returntype 有点困惑。有时候我请参阅:jQuery,现在我正在使用 => void。两者有什么区别?

最佳答案

在您的示例中,它们都使用冒号声明...

让我们把它分成两半!

没有类型声明...

interface JQuery {
addTemplateSetup
}

类型声明...

: (func: Function, priority: bool) =>void;

如果你用 is 这个词代替 : 就像在说

The property named 'addTemplateSetUp' : A function accepting two parameters of specific types and not returning a value

这是一个示例,其中包含两个实际上相同的接口(interface),但一个使用 => 而另一个不使用。 TypeScript 对两者的处理方式相同,因此这实际上取决于开发人员的偏好。

interface JQuery {
addTemplateSetup: (func: Function, priority: bool) =>void;
}

interface Identical {
addTemplateSetup(func: Function, priority: bool): void;
}

class ImplementsEither implements JQuery {
addTemplateSetup (func: Function, priority: bool) : void {

}
}

如果您喜欢几乎用英文阅读,您可以使用 => 样式声明。

如果您希望您的界面看起来更像实现,您可以使用第二种风格。

关于javascript - 使用: and => for the return type with a TypeScript function?有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13173001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com