Depends how the pet information is stored, you could give each pet an id and when one is clicked a variable is set to that id, you can use that variable to get information about the pet.
|
So I am currently developing an App and part of it is in in JavaScript (yay for me)
Basically lets say i have this
then i have thisCode:function pets (breed, name, age) { this.breed = breed; this.name = name; this.age = age; }
So i have these displayed using the front-end markup of this service i am using and I am able to display all the pets by doing the equivilent of an for() function except in this other language. Now I am trying to make it so when I click on one of these pets, i can just console.log(); the name, breed, age etc... But how would i go about that? I would need to create a completely different function to check to see what pet it is like so:Code:var differentPets = Observable( new pets(breed, name, age); new pets(breed, name, age); );
But how can i make it so that the computer knows it is that... I was thinking to have a boolean within the original consutrctor for pets and then when it is clicked change the value of the boolean and call the function to search to see which of the pets were clicked, but is that efficient? or is there an easier way to do it?Code:function checkPet() { }
Depends how the pet information is stored, you could give each pet an id and when one is clicked a variable is set to that id, you can use that variable to get information about the pet.
A boolean has only two states true (1) or false (0), so you could give all the buttons a boolean and turn to true if clicked the loop through them all and check the boolean however that's not particularly efficient.
If you change
and give every pet a unique idCode:function pets (id, breed, name, age) { this.id = id; this.breed = breed; this.name = name; this.age = age; }
then create a variable
Then when a pet is clicked you can simply setCode:var clickedPetId = 0;
then use it to gain the info you need.Code:clickPetId = id;
It requires more storage space but is more efficient when it comes to processing as there is no need to loop through all the animals.
Although unless you're loading millions of animals I doubt it will make a negligible difference.
« Previous Thread | Next Thread » |
Thread Information |
Users Browsing this ThreadThere are currently 1 users browsing this thread. (0 members and 1 guests) |