473,396 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Acessing same object properties between several functions

Hi,

I have an object that contains the main data for my script. For example:
Expand|Select|Wrap|Line Numbers
  1. function Invoice() {
  2.     this.name="bob"
  3.     this.email="abc@example.com"
  4. }
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:

Expand|Select|Wrap|Line Numbers
  1. function process_input(element) {
  2.     // This function checks input and gives and assigns valid input to a 
  3.     // namesake property of the Invoice object
  4.  
  5.     var regex = new Patterns(); // an object full of regexs to match against input
  6.     var order = new Invoice();
  7.  
  8.     var value = document.getElementById(element).value;
  9.  
  10.     var i;
  11.     for (i in regex) {
  12.         if (element === i && value.match(regex[i])) {
  13.             order[element] = value;
  14.             return true;
  15.         }    
  16.     }
  17.     return false; 
  18. }
  19.  
  20. function Invoice() {
  21.     // Class with properties created by process_input    
  22. }
  23.  
  24.  
  25. function main() {
  26.  
  27.     var myForm = document.getElementById('orderform');
  28.     for (var i = 0; i < myForm.elements.length; ++i) {
  29.         var e = myForm.elements[i];  
  30.         // Ignore element unless it is a 'text input' box or a 'select' combo box
  31.         if (e.type === "text" || e.type === "select-one") {            
  32.             if (process_input(e.id)) {
  33.                 e.setAttribute("class", "valid");
  34.             } 
  35.         }
  36.     }
  37.  
  38. }
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 :-)
Aug 16 '08 #1
2 1181
rnd me
427 Expert 256MB
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_input" function.
Aug 16 '08 #2
Thanks a lot :)

I've made a global instance of the object for now. (I.E:
Expand|Select|Wrap|Line Numbers
  1. INVOICE = function() {
  2. }
)

How would one usually catch a returned object in another function? Would I use something like
Expand|Select|Wrap|Line Numbers
  1. 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. :)
Aug 16 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Nitec Dev | last post by:
Our setup: server1 ASP database running on IIS5, Outlook 2000 and SQL 2000 on Windows 2000. Server2 runs Windows 2000 with Exchange 2000 using CDO 1.2 MAPI Sessions. Clients use IE5/6 and had...
2
by: Peter Nofelt | last post by:
Hey all, I am running into an issue. My situation is that I wish to copy the contents of one listbox to an array, sort it by innerText value, and write the sorted options into a new listbox. ...
2
by: Lachlan Hunt | last post by:
Hi, In JavaScript 1.5, objects can use special getter and setter functions for properties. However, these only seem to be implemented in Gecko and, AFAICT, don't seem to be part of ECMAScript. ...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
17
by: Jef Driesen | last post by:
Suppose I have a datastructure (actually it's a graph) with one template parameter (the property P for each edge and vertex): struct graph<P>; struct vertex<P>; struct edge<P>; I also have...
8
by: RSH | last post by:
Hi, I have a situation where I have a Parent Object (Company) which has several public properties. The Company Object also creates an Employees object which has its ow set of functions and...
21
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
7
by: d d | last post by:
I have an array of objects that start out looking like this: var ra=; I want to be able to access the array by index number from some code, and by the name property from other code, as if it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.