所以我尝试使用闭包创建具有原型(prototype)继承的对象,而无需使用“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
我做错了什么?你认为我可以为此使用一些更好的编码方式
我感谢所有的帮助。
最佳答案
如果您想使用原型(prototype)继承,我们必须使用 new 关键字。
最好使用示例:-
http://www.klauskomenda.com/code/javascript-inheritance-by-example/
关于javascript - 如何在不使用 "new"运算符的情况下使用原型(prototype)继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12554443/