Working with objects is the only way to manipulate arrays or any other object in the JavaScript language. For example, it is necessary to implement a client list. The object can be implemented as follows:
Function CreateClients (name, address, telephone, rent)
{
this.name = name;
this.address = address;
this. phone = phone;
this.income = income;
}
The property “this” specifies the current object as a source of values passed to the function. Now, just create the object:
Maria = New CreateClients (‘Maria Aparecida’, ‘Guillotine Rua dos Patos, S / N’, ‘332-1148’, 1300)
To access the properties of Maria object, simply use the following syntax:
Maria.name -> returns ‘Maria Aparecida’
Maria.address -> returns ‘Guillotine Rua dos Patos, S / N’
Maria.phone -> returns ‘332-1148’
Maria.income -> returns in 1300
Another way to reference the properties of Maria’s object is:
Maria [0], Maria [1] Maria [2] Maria [3]
To create arrays, a more practical approach could be the following:
Function Matrix (n)
{
this.length = n
for (var counter = 1; counter <= n; conatdor = counter + 1)
{
this [counter] = “”
}
}
To create our array, then we would use the following command:
Month = matrix (12)
The next step would include only the data:
Month [1] = ‘January’
Month [2] = ‘February’
…
Month [12] = ‘December’
Finally, it is also possible to use both methods simultaneously:
New customers = Array (3)
Clients [1] = New CreateClients (“Charlene”, “Street A 51”, “331-0376”, 1150)
Customers [2] = New CreateClients (“Joseph”, “Street of Covenants, S / N,” “332-2781”, 950)
Customers [3] = New CreateClients (“James Manson,” “Joy Street, 171 ‘, 1000)
So obtaining the following scenario:
Customers [1] .name = “Charlene”
Customers [2] .phone = “332-2781”
Customers [3] = null .phone
Visit our blog and check out more content!
You might also like…