为模拟信用卡申请发行简单的唯一 ID
issuing simple unique ID for a mock credit card application
我在向我的 creditUser class 颁发唯一 ID 时遇到了麻烦。我不确定我是否应该使用设置为 0 的全局变量来执行此操作,或者我是否应该在构造函数中将其设置为 0...或在构造函数下方。我也不清楚如何定义余额(欠款),或者更确切地说,也只是将其设置为 0。我不知道应该在哪里声明。非常感谢任何帮助!这是 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);
JavaScript 提供了很多创建对象的方法...这是我熟悉的一种方法,使用函数作为对象,然后创建该函数的新实例作为您的独特对象.
// 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 );
我在向我的 creditUser class 颁发唯一 ID 时遇到了麻烦。我不确定我是否应该使用设置为 0 的全局变量来执行此操作,或者我是否应该在构造函数中将其设置为 0...或在构造函数下方。我也不清楚如何定义余额(欠款),或者更确切地说,也只是将其设置为 0。我不知道应该在哪里声明。非常感谢任何帮助!这是 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);
JavaScript 提供了很多创建对象的方法...这是我熟悉的一种方法,使用函数作为对象,然后创建该函数的新实例作为您的独特对象.
// 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 );