Acessing same object properties between several functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • metafizzical
    New Member
    • Aug 2008
    • 4

    Acessing same object properties between several functions

    Hi,

    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'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:

    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");
    			} 
    		}
    	}
    	
    }
    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 :-)
  • rnd me
    Recognized Expert Contributor
    • Jun 2007
    • 427

    #2
    you will have to do one of the following.

    -make the object instance a global, or a property of a global.

    -return the order object and catch it in another function.

    -wrap the entire code in one outside function to close the object instance. you would still have to move the "var order=" outside the "process_in put" function.

    Comment

    • metafizzical
      New Member
      • Aug 2008
      • 4

      #3
      Thanks a lot :)

      I've made a global instance of the object for now. (I.E:
      Code:
      INVOICE = function() {
      }
      )

      How would one usually catch a returned object in another function? Would I use something like
      Code:
      var foo = new Object(some_function)
      ? I have tried to return an object and place it in another object previously, and also an array without success - however, this may well have been a syntax problem on my part. :)

      Comment

      Working...