介绍
首先( type dipliases )或接口(在谈到界面,我确实考虑了golang thive golang thimecript,而不是Typescript < /em>)与类型安全有关,这是JavaScript中缺乏功能(以下简称为JS)。
词汇表
与Golang相比 接口在JS上下文中可以想象为包含this.
值的方法签名,而函数的签名域将消耗某些struct的(〜对象)的“方案”。
javaScript中的接口确实存在,但在不同的类型中,较少的形状:
globalThis.someINTERFACE = {
Area: function (x, y) {
[this.x, this.y] = [x, y]
console.log("Area: ", this.x * this.y)
return this.x * this.y;
},
Radius: function (radius) {
console.log("Radius: ", radius * radius * Math.PI)
return (radius * radius * Math.PI);
}
}
// some mocked type-safety behaviour
const int = Number; // some mocked underlying type
globalThis.someSTRUCT = {
someINTERFACE, /** @{optional} : example embedding an interface */
x: int,
y: int,
}
// .call() bypasser
function shapeDetails(structReference, structBase, ...rest){
structReference.call(structBase, ...rest)
}
// mocked main function as entry point loading stack in memory
function main(){
const [Area, Radius] = [
shapeDetails(someSTRUCT.someINTERFACE.Area, someINTERFACE, 2, 3),
shapeDetails(someSTRUCT.someINTERFACE.Radius, someINTERFACE, 2, 3),
]
for (calculations of [Area, Radius]) {
return calculations;
}
}
main()
/**
stdout:
Area: 6
Radius: 12.566370614359172
*/