I’m giving JavaScript OOP a swirl for the first try and I’m getting this error:
Uncaught TypeError: Cannot call method ‘addWeight’ of undefined
It is complaning about a method that is under weight.webdb.addWeight which is being called from a global function 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);
}
Your local variable
weightdefined in the function is shadowing theweightvariable in the outer scope. Just call the function-local variable something else, likeweight_elem: