好吧,所以我是一個完全新手到JavaScript的面向對象,顯然。我以爲我明白了,但看來我只知道一小部分。無論如何,我想要做的是設置一個對象來存儲和返回XML輸入的數據,通過使用相當簡單的字符串來檢索數據。我想用類似於reader.getItem().getSubItem()
或類似的字符串檢索數據。在Javascript中嵌套對象 - 匿名不是一個函數錯誤
下面是我嘗試的一個例子,但每次我嘗試撥打電話fr.getType().isTexture()
時都會收到錯誤anonymous is not a function
,所以很明顯,我需要更改某些內容。
//Create the object by passing an XML element containing sub-elements
var fr = new FeatureReader(test.child(i));
alert(fr.getName()); //returns the object's name
alert(fr.getType().isTexture()); //"anonymous is not a function" error
function FeatureReader(feature) {
var feat = feature;
this.getName = function() {
return feat.name;
};
this.getType = new function() {
this.isTexture = new function() {
if (feat.type.texture == "yes") {
return true;
}
return false;
};
this.isModel = new function() {
if (feat.type.model == "yes") {
return true;
}
return false;
};
};
}
現在,很明顯,我可以只取出this.isTexture
和this.isModel
在周圍的this.getType = function() {}
得到我的數據,但對於學習的東西的緣故,我想看看它是如何建議我設置對象直到使用類似於第一段和第二段中提到的字符串來獲取返回的值。
這應該有助於http://stackoverflow.com/questions/1646698/what-is-the-new-keyword-in-javascript – elclanrs