【问题标题】:Firefox cannot make an instance of class generated with TypeScriptFirefox 无法使用 TypeScript 生成类的实例
【发布时间】:2013-01-24 09:48:16
【问题描述】:

我有一个用于包装 Grid 控件的简单类,我将它放在名为 Components 的模块中,当我在 Firefox 14.0.1 中对其进行测试时,我得到:

TypeError: Components.Grid 不是构造函数@http://...

这里是 TypeScript 生成的代码:

var Components;
(function (Components) {
    Components.gridDefaults = {
        datatype: "json",
        autowidth: true,
        rowNum: 30,
        rowList: [
            10, 
            20, 
            30
        ],
        viewrecords: true,
        sortorder: "asc",
        pager: "#grid-pager"
    };
    var Grid = (function () {
        function Grid(selector, options) {
            var opts = Components.gridDefaults;
            if(options !== undefined && options !== null) {
                $.extend(opts, options);
            }
            this.grid = $(selector).jqGrid(opts);
        }
        Grid.prototype.setToolbar = function (options) {
            var pager = this.grid.getGridParam().pager;
            if(pager !== undefined && pager !== null) {
                this.grid.navGrid(pager, options);
            }
            return this;
        };
        Grid.prototype.jqGrid = function () {
            return this.grid;
        };
        Grid.prototype.filter = function (criteria) {
            this.grid.setGridParam({
                page: 1,
                postData: criteria
            });
            this.reload();
        };
        Grid.prototype.reload = function () {
            this.grid.trigger("reloadGrid");
        };
        return Grid;
    })();
    Components.Grid = Grid;    
})(Components || (Components = {}));

//@ sourceMappingURL=grid-component.js.map

这里是 Firefox 抱怨缺少构造函数的那一行:

var grid = new Components.Grid("#grid-container", {
            url: "/Home/Data",
            colNames: ['Inv No', 'Date', 'Client', 'Amount', 'Tax', 'Total', 'Notes'],
            colModel: [
                { name: 'id', index: 'id', width: 55 },
                { name: 'invdate', index: 'invdate', width: 90, jsonmap: "invdate" },
                { name: 'name', index: 'name asc, invdate', width: 100 },
                { name: 'amount', index: 'amount', width: 80, align: "right" },
                { name: 'tax', index: 'tax', width: 80, align: "right" },
                { name: 'total', index: 'total', width: 80, align: "right" },
                { name: 'note', index: 'note', width: 150, sortable: false }
            ],
            jsonReader: {
                id: "id",
                repeatitems: false
            }
        });

我想听听@TypeScript 团队的消息。

【问题讨论】:

    标签: javascript firefox typescript


    【解决方案1】:

    我不是 TypeScript 团队的成员,也从未使用过它。但这不是 TypeScript 问题,我相信。问题是您试图覆盖 Firefox 作为本机包含的内容,即Components

    如果你改成

    var Comps;
    (function (Components) {  // keep the local name or change to Comps, as you like
        Components.gridDefaults = {
        // ... 
    })(Comps || (Comps = {}));
    var grid = new Comps.Grid("#grid-container", { /* ... */ });
    

    我认为它会更好。

    我认为这意味着更改您的 Typescript 模块名称。但我对此一无所知。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    • 2019-11-04
    • 1970-01-01
    相关资源
    最近更新 更多