- Home /
Objects in Javascript Help!
//Define the class
class author{
function author(a:String,b:String,c:int)
{
this.name = a;
this.lastname = b;
this.age = c;
}
//Declaring the variables
var name:String;
var lastname:String;
var age:int;
}
//I make a new object
newBook = new author("J.R.","Tolkien",60);
//I make other new object
newBook = new author("Agatha","Christie",42);
in this code the variable "newBook" to save the whole object, returns to save only the last one that was created...
Any help???
Answer by Proclyon · Dec 06, 2010 at 06:07 PM
Variables are like a lockbox for one specific unique item, and you know what kind of item goes in. Just like a bar of soap goes into the little plastic soapbox. You put another in it simply will not fit.
HOWEVER a smart mechanism in programming for quicker programming is that it automatically removes the soap and puts the new one in. The other is "magically" removed.
newBook = "someBook" Box --> Insert Soap A
newBook = "someBook" Box --> Remove Soap A --> Insert Soap B
You can only have one inside that variable sorry. Doing otherwise is impossible for your container (the soapbox).
What you need is a bigger box. An array or list.
First one you put in is the oldest, and the last the youngest. And if you meddle with the order you need to clean it up again(sorting algorithms).
You can learn how to use such a container following this link here. Remember that to keep track of a collection you will need to know where something is (even the small ones). To do so use the index number (starting usually at 0) to acces the place where it is, and do with it what you want. Everything you do with it inside the array/list/new bigger box will change with it unless you take it outside of there by putting it in another container.
The less complicated and sophisticated way of doing this is NOT keeping a collection but a pile of junk.
newBook_1 = "someBook" newBook_2 = "someOtherBook" newBook_3 = "someBook"
newBook_3 well that name really doesn't help you find it back does it? Nor does it let you see that you already had that book, which also happens in the collections and/or containers. So programming lesson 1 as I was taught I will share with you. If you don't know what is where and for which reason, you need to find out and fix it and make sure it doesn't happen again.
EDIT 08:41 GMT+1 12/07/2010:
This is a pretty good one with examples: http://www.hunlock.com/blogs/Mastering_Javascript_Arrays
This has a sorting explanation with it aswell, could be usefull: http://www.tizag.com/javascriptT/javascriptarray.php
And the perfect one always being this: Google + search phrase: Just include Javascript + Array and maybe tutorial and there is simply no end to it.
Answer by Eric5h5 · Dec 06, 2010 at 05:47 PM
Use a different variable...when you assign a new value to "newBook", the old value is gone.
Answer by raul corrales · Dec 06, 2010 at 05:57 PM
but i want... the variable newBook not change the name
Answer by raul corrales · Dec 06, 2010 at 06:15 PM
Proclyon , you can put me an example code using an array? Please, and thanks in advance Thank for answerme
Actually I don't think that's a good idea. Since you like to learn basics you need someone who knows it from A to Z so you don't learn something incorrectly, I can do so in C# but not in Javascript. But I will edit my answer and give you some extra links to help you get started.
oh and yes please do post such things as a comment below my answer. Stackexchange works differently compared to regular forums. $$anonymous$$ost of it is explained here http://answers.unity3d.com/faq
Your answer
Follow this Question
Related Questions
Literal Notation Object Creation in unity? 0 Answers
Collision between objects 2 Answers
Organizing variables in the inspector 5 Answers
Calling inherited constructor 3 Answers