3
是什么在接口定义像函数之间的区别:打字稿:差异=>和:接口函数声明
interface IMyInterface1 {
functionName(value: string): void;
}
和
interface IMyInterface2 {
functionName: (value: string) => void;
}
是什么在接口定义像函数之间的区别:打字稿:差异=>和:接口函数声明
interface IMyInterface1 {
functionName(value: string): void;
}
和
interface IMyInterface2 {
functionName: (value: string) => void;
}
有没有真正的区别就在这里,你可以使用哪一个你感觉更舒适。
有了这样说,我考虑的第一个暗示一个类的方法,而第二个是它是一个功能的部件:
class MyClass1 implements IMyInterface1 {
functionName(value: string): void {}
}
class MyClass2 implements IMyInterface2 {
functionName = function(value: string): void {}
}
这仅仅是一个个人喜好,编译器将让你做相反的好:
class MyClass1 implements IMyInterface1 {
functionName = function(value: string): void {}
}
class MyClass2 implements IMyInterface2 {
functionName(value: string): void {}
}
以类似的方式,这两个符号是相同的:
let obj1 = {
functionName() {}
}
let obj2 = {
functionName: function() {}
}
因为它们都编译为:
var obj1 = {
functionName: function() { }
};
var obj2 = {
functionName: function() { }
};
[Function property vs method]的可能重复(https://stackoverflow.com/questions/39156315/function-property-vs-method) –