There are two ways to dynamically add an element to the end of a JavaScript array
Sometimes you may need variables that are available to every function in your script. While it is tempting to use global variables to achieve this kind of scope, doing so can cause unpredictable results and spaghetti code. If you create your own object, define your properties and methods, and then access them via a clean, name-spaced syntax, you control the scope as well as the code’s behavior.
Example # 1:
Here are a few examples of using global variables. In each case, it is very easy to lose track of the value of these variables throughout your script, as well as which functions have access to them.
1 2 3 |
var clientName = "Bob Jones"; var clientAccoutNumber = "123456" var clientPhone = "(212) 555-1212" |
You may want to access these variables from multiple functions in your code, and in various scenarios, change the value of those functions. This is certainly possible, but there are better ways to achieve the same functionality.
Example # 2:
In this example, we create a custom object called “bankClient”. We then define the properties of this object.
1 2 3 4 5 |
var bankClient = { "name" : '', "accountNumber" : '', "phoneNumber" : '' } |
In this example, there are two ways that we could access these variables:
Example # 2A
- object.property
- object[‘property’]
Example # 2B
1 2 |
bankClient.name = "Bob Jones"; bankClient.['name'] = "Bob Jones"; |
Either one of the above approaches will work just fine.
Example # 3:
You can also define a method for your object. A method would be a function that you define within the object, and then call by using the same name-spaced syntax. In the example below, we expand our object by adding a method. This method returns the value of the client account number. You may notice the use of the “this” keyword. In such a case, “this” refers to the object who’s context we are currently in, which would be “bankClient”. This is something you’ll see often when working with objects in JavaScript.
That value is hard-coded in the object definition, but then notice how we change the value of the property, and then retrieve it. In the same manner, the property “name” is at first empty, but we assign a value to it, and then grab that value (i.e. “Roger Sterling”).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
var bankClient = { "name" : '', "accountNumber" : '123456', "phoneNumber" : '', "getAccountNumber" : function() { return this.accountNumber ; } } console.log( bankClient.getAccountNumber() ); bankClient.accountNumber = '111-222-333'; console.log( 'Account # changed to: ' + bankClient.getAccountNumber() ); bankClient['name'] = 'Roger Sterling'; console.log( 'Client Name: ' + bankClient.name ); |
The output for Example # 3 would be:
123456 Account # changed to: 111-222-333 Client Name: Roger SterlingSummary:
Creating your own custom object is a good way to avoid cluttering up the global namespace. It is also an improved method of keeping tabs on your variables as they become properties of the object. You can define methods for your object and access them the same way. In doing this, you create organized code that’s easier to read, maintain, and extend.
Helpful Links about JavaScript Objects
http://www.w3schools.com/js/js_objects.asp