Javascript 如何在没有“new”操作符的情况下使用原型继承

Javascript 如何在没有“new”操作符的情况下使用原型继承,javascript,closures,prototype,Javascript,Closures,Prototype,因此,我尝试在没有使用闭包的新操作符的情况下创建具有原型继承的对象。 我的代码是: var TABLE=function (tableId) { var table=(document.getElementById(tableId)||{}), colName=[], rows, cols, selectedRow; //private methods var updateRows=function(){ //Code that upd

因此,我尝试在没有使用闭包的新操作符的情况下创建具有原型继承的对象。 我的代码是:

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
我做错了什么?你认为我可以用更好的方式来编码吗


我感谢所有的帮助

如果您想使用原型继承,我们必须使用new关键字

最好与示例一起使用:-


原型继承显然适用于新的继承,所以省略它然后抱怨是行不通的。我想不带轮胎开车。太糟糕了。您希望在这种情况下使用原型的原因是什么?在我看来,如果您只删除方法结构中的“prototype”键,那么您的代码将按预期工作?基本上,您所做的事情似乎不需要任何形式的继承……看看Douglas Crockford的“寄生继承:我刚刚阅读了JavaScript:the Good Parts”。他实际上建议我们创建对象,避免使用新操作符。所以我想这样做,并有原型继承。我不是javascript方面的专家,但我想成为Douglas Crockford实际建议的专家之一,在需要的地方使用浅层继承。从上面我所看到的,你不需要继承。。。但如果你这样做了,那么使用“新”和使用.prototype到一个简单的级别会更好。为了增加我自己的个人记录,我已经写javascript应用程序、插件和脚本13年了。。我从来都不需要使用.prototype来实现我的目标,但如果没有“新的”,我不会走得很远。最接近继承的是一个对象实例被许多其他实例静态使用。