0
我有一个'model
'类/原型定义如下,它有一个名为'given
'的子类,它尝试访问模型类的方法'getNodes()
'。如何访问JavaScript中子类中父类的方法
但它给出'this.getNodes
'的例外说未定义。
var model = {
constructor: function(/*string*/ mode, /*string*/ name, /*object*/ properties) {
this._mode = mode;
this.beginX = 100;
this.beginY = 100;
this.nodeWidth = 200;
this.nodeHeight = 200;
this.x = this.beginX;
this.y = this.beginY;
this.lastNodeVisible = null;
this.ID = 1;
this.taskName = name;
this.properties = properties;
this.checkedNodes = new Array();
// this.model = #call_build_method;
/*
add subclasses with model accessors
*/
this.given = {
getNodes: this.getNodes,
setNodeName: this.setNodeName
};
},
getNodes: function() {
// Summary: returns an array containing the nodes in the given model
return #someobject;
},
}
当你周围有一个函数'this'三思而后行。这个'取决于如何调用这个函数。在你的情况下,你需要缓存它,比如'var self = this'在更高的范围内。请参阅http://stackoverflow.com/questions/3127429/javascript-this-keyword – elclanrs
任何代码示例请 – Pradeep
如何调用该构造函数? – Bergi