1

したがって、クロージャを使用して「new」演算子を使用せずにプロトタイプの継承を持つオブジェクトを作成しようとしています。私のコードは次のとおりです。

var TABLE=function (tableId) {
var table=(document.getElementById(tableId)||{}),
    colName=[],
    rows,
    cols,
    selectedRow;

//private methods

    var updateRows=function(){
        //Code that updates the number of row of the table
    }
    updateRows();

//Declare the public methods for the object

var methods={
    prototype:{
        cols:function () {
        return cols;
        },
        rows:function () {
            return rows;
        }
    }
};
    return methods;
}

var table=Table("Inventory")
alert(table.rows()) //I get undefined
alert(table.prototype.rows()) //I get the actual number of rows

私は何が間違っているのですか?あなたは私がこれのためにコーディングのいくつかのより良い方法を使うことができると思いますか

私はすべての助けに感謝します。

4

1 に答える 1

0

プロトタイプの継承を使用する場合は、 new キーワードを使用する必要があります。

例で使用するのが最適です:-

http://www.klauskomenda.com/code/javascript-inheritance-by-example/

于 2012-09-23T17:24:36.407 に答える