作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有以下代码:
///<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/
我是一名优秀的程序员,十分优秀!