意外的标记; '构造函数,函数,访问器或变量'预期

时间:2015-07-14 00:52:45

标签: typescript

抱歉新手问题,但我正在尝试学习类型脚本。

我有以下课程

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

1 个答案:

答案 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);
    };

} 

我还在我提供的代码中为您的代码做了其他修复: