Search code examples
javascriptconstructorglobal-variablescounterdefault

issuing simple unique ID for a mock credit card application


I am having trouble getting anywhere with issuing a unique id to my creditUser class. I am not sure if I'm supposed to do this with a global variable that is set to 0 or if I'm supposed to set it to 0 in the constructor... or below the constructor. I also am unclear on how to define the balance (owed) or rather, just set it to 0 as well. I don't know where this should be declared. Any and all help is much appreciated! Here is a version of it on repl.it- https://repl.it/@rockomatthews/Objects-Project

//DO I SET THEM HERE?
var idCounter = 0;
var balance = 0;

    var CreditUser = class {

  constructor(
    id,
    firstName,
    lastName,
    income,
    limit,
    arp,
    balance
  ) {
      //OR DO I SET THEM HERE?
      this.id = id;
      this.firstName = firstName;
      this.lastName = lastName;
      this.income = income;
      this.limit = limit;
      this.balance = balance;
    }
};

//OR DO I SET THEM DOWN HERE SOMEWHERE TO 0?
var creditUser = new CreditUser (this.id, this.firstName, this.lastName, 
this.income, this.password);
  this.creditUser.id = this.id;
  this.creditUser.firstName = prompt("What's your first name?");
    console.log(this.creditUser.firstName);
  this.creditUser.lastName = prompt("What's your last name?");
    console.log(this.creditUser.lastName);
  this.creditUser.income = prompt("What's is your income this year?");
    console.log(this.creditUser.income);
  this.creditUser.limit = this.creditUser.income * .2;
  console.log(this.creditUser.limit);
    console.log(this.creditUser);

Solution

  • JavaScript provides a lot of ways to create objects... This is one way that I'm comfortable with, using a function as the object and then creating new instances of the function as your unique objects.

    // declare the parameters to be saved in the function
    // declaration, so you can capture them in the object
    var CreditUser = function( id, firstName, lastName, income, limit, balance ){
    // use the OR statement to assign a default value
    // to the parameters, if nothing is provided
      this.id        = id        || 0;
      this.firstName = firstName || '';
      this.lastName  = lastName  || '';
      this.income    = income    || 0;
      this.limit     = limit     || 0;
      this.balance   = balance   || 0;
    };
    
    // User a different variable name, to avoid 
    // the object being overwritten or vice versa
    var user1 = new CreditUser();
    // change this. to the variable name of the new
    // object.  'this' at this level means the document
        user1.firstName = prompt("What's your first name?");
        user1.lastName  = prompt("What's your last name?");
        user1.income    = prompt("What's is your income this year?");
        user1.limit     = user1.income * .2;
    
    // user2 references the same object, but the data is
    // separate from user1
    var user2 = new CreditUser();
        user2.firstName = prompt("What's your first name?");
        user2.lastName  = prompt("What's your last name?");
      
    // check the results of the user1 object vs. 
    // the user2 object to prove they don't overwrite each
    // other
    console.log( user1 );
    console.log( user2 );