Hi,
I have an object that contains the main data for my script. For example:
I'm trying to access and edit the object properties through several different functions - However, I don't seem to know the Right Way to do this ;)
I've been browsing online but haven't been able to find an example - or least not one I recognise due to my OO and JS newbie-ness. Here's some sample code:
The above code appears to work just fine at present. However, I'd like to add another function to some calculations and print the properties of the Invoice object.
Should I be looking at making process_order() a method of the Invoice object? Or do I need the Invoice object to be accessible globally?
Hopefully, I'm making sense :-)
I have an object that contains the main data for my script. For example:
Code:
function Invoice() {
this.name="bob"
this.email="abc@example.com"
}
I've been browsing online but haven't been able to find an example - or least not one I recognise due to my OO and JS newbie-ness. Here's some sample code:
Code:
function process_input(element) {
// This function checks input and gives and assigns valid input to a
// namesake property of the Invoice object
var regex = new Patterns(); // an object full of regexs to match against input
var order = new Invoice();
var value = document.getElementById(element).value;
var i;
for (i in regex) {
if (element === i && value.match(regex[i])) {
order[element] = value;
return true;
}
}
return false;
}
function Invoice() {
// Class with properties created by process_input
}
function main() {
var myForm = document.getElementById('orderform');
for (var i = 0; i < myForm.elements.length; ++i) {
var e = myForm.elements[i];
// Ignore element unless it is a 'text input' box or a 'select' combo box
if (e.type === "text" || e.type === "select-one") {
if (process_input(e.id)) {
e.setAttribute("class", "valid");
}
}
}
}
Should I be looking at making process_order() a method of the Invoice object? Or do I need the Invoice object to be accessible globally?
Hopefully, I'm making sense :-)
Comment