抱歉新手问题,但我正在尝试学习类型脚本。
我有以下课程
class indexGridFunctions
{
//Error on the var
var blocksPerRow: (windowWidth:number)=>number
= function (windowWidth)
{
return Math.floor(windowWidth / 12);
};
var blocksPerColumn: (windowHeight: number) => number
= function (windowHeight)
{
return Math.floor(windowHeight / 17);
};
shirtsToDisplay: () => number
= function ()
{
return blocksPerRow * blocksPerColumn;
};
}
我在第一个var时遇到错误。错误是“意外令牌;'构造函数,函数,访问器或变量'预期”。
我做错了什么?
TIA
答案 0 :(得分:6)
请勿使用var
。它在类体内的语法无效。固定代码:
class indexGridFunctions {
blocksPerRow: (windowWidth: number) => number
= function (windowWidth) {
return Math.floor(windowWidth / 12);
};
blocksPerColumn: (windowHeight: number) => number
= function (windowHeight) {
return Math.floor(windowHeight / 17);
};
shirtsToDisplay: () => number
= () => {
return this.blocksPerRow(123) * this.blocksPerColumn(123);
};
}
我还在我提供的代码中为您的代码做了其他修复:
blocksPerRow
和blocksPerColumn
未定义。使用this.
。它的一个功能就是称之为(123)
。 arrow ()=>
(更多https://www.youtube.com/watch?v=tvocUcbCupA)function
优先于=