函数属性 vs 方法 [英] Function property vs method

查看:30
本文介绍了函数属性 vs 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

定义接口方法之间有什么实际区别:

What practical differences are there between defining an interface method:

interface Foo {
    bar(): void;
}

并定义具有函数类型的属性:

and defining a property with a function type:

interface Foo {
    bar: () => void;
}

?

推荐答案

如果只有这些声明,则它们是相同的.

If these are the only declarations, these are identical.

唯一的区别是您可以增强第二个声明中的第一个表单以添加新签名:

The only difference is that you can augment the first form in a second declaration to add new signatures:

// Somewhere
interface Foo {
  bar(): void;
}

// Somewhere else
interface Foo {
  bar(s: number): void;
}

// Elsewhere
let x: Foo = ...
x.bar(32); // OK

这篇关于函数属性 vs 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆