Add function - create new object


#1

Hi, is there any way to create a new variable when running the add function, instead of just storing the various fields as the last item in an array?

I suppose, if I understand the concept correctly, this would constitute creating and declaring a new object when I run the code, for example, john. Is that possible?


#2

Part of the idea is to get away from named objects as much as possible, and grouping in a single array or object is much easier to search, amend, append or prepend, or delete entries from. Variables on the other hand can only be nulled, never deleted, as such.

One way to cater to both, global and reference object is with a constructor.

function Person(firstName,lastName,email,phoneNumber){
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.phoneNumber = phoneNumber;
}

Now if we wish to instantiate a global variable reference,

john = new Person(" ... ", " ... ", " ... ", " ... ");

but if we just want to keep adding to the array,

contacts.push(new Person(" ... ", " ... ", " ... ", " ... "));

#3

Thank you! This is helpful.

Would Codecademy put together classes on the abstraction concept?


#4

We may have jumped ahead. Constructor functions come up in these modules. Keep forging ahead...