我給的JavaScript OOP的第一次嘗試一個漩渦,我得到這個錯誤方法「XXX」:遺漏的類型錯誤:無法調用未定義
Uncaught TypeError: Cannot call method 'addWeight' of undefined
據complaning約是下一個方法weight.webdb.addWeight
從全局函數addWeight
被調用。
var weight = {};
weight.webdb = {};
weight.webdb.db = null;
weight.webdb.addWeight = function() {
var db = weight.webdb.db;
db.transaction(function(tx) {
var addedOn = new Date();
tx.executeSql('INSERT INTO weight(input,comment,date) VALUES (?,?,?)', [inputWeight, inputComment, addedOn], weight.webdb.onSuccess, weight.webdb.onError);
});
};
function addWeight(){
var weight = document.getElementById('inputWeight');
var comment = document.getElementById('inputComment');
weight.webdb.addWeight(weight.value,comment.value);
}
演示:http://jsfiddle.net/SMbL3/1/
什麼是'webdb'?哦,我必須打開小提琴才能看到你在說什麼?無論如何,您如何將DOM元素與您的任意命名對象相關聯? –
是什麼讓你認爲「inputWeight」DOM元素具有「webdb」屬性? –