我正在为JS课程做作业。
说明说: setHrs()函数接受工作小时数的输入。函数应该将输入保存到其实例变量名称hrs。没有回报。
好的,所以我几乎陷入困境。下面是我的代码,我在其中定义员工,给他们属性,在Employee之间共享这些属性 - >员工1 - >和Employee2
一旦我找到了Employees,我需要在Hourly()中创建函数,然后我可以在程序中稍后调用; getRate()和getHrs()。
我需要为this.rate和this.hrs分配新值的指针,这是我的代码:
//Prince of Wales, Bed and Breakfast Resort
function Employee(id, name, hiredate, position){
this.id = id;
this. name = name;
this.hiredate = hiredate;
this.position = position;
}
Employee.prototype.hired = "Employed";
var Employee1 = new Employee("4", "Blackadder",
"06-03-1902", "butler");
var Employee2 = new Employee("5", "Baldrick",
Employee1.hiredate, "who knows");
// printed out to test prototype works... everything above can be printed
function Hourly(rate, hrs) {
this.rate = 0;
this.hrs = 0;
this.setHrs = function(){
// set Employee1 hours
// set Employee2 hours
}
this.setRate = function() {
// set Employee1 rate
// set Employee2 rate
}
}
Hourly.prototype = new Employee();
Employee1.prototype.hrs = 50;
Employee2.prototype.hrs = 25;
Employee1.prototype.rate = 4;
Employee2.prototype.rate = 1;
console.log(
"Name : " , Employee1.name ,
" Hourly Rate : ", Employee1.setRate(),
" Hours Worked : ", Employee1.setHrs(),
);
console.log(
"Name : " , Employee2.name ,
" Hourly Rate : ", Employee2.setRate(),
" Hours Worked : ", Employee2.setHrs(),
);
我曾想过其他可能有用的东西,但事实并非如此,但我可能会走上正轨???
this.setHrs = function(){
Employee1.hrs = 50;
Employee2.hrs = 25;
}
this.setRate = function() {
Employee1.rate = 4;
Employee2.rate = 1;
}
我也想过,也许rate和hrs应该是数组,我可以将值推送给它们,但我尝试的所有内容都没有为数组添加值。另外,如果我这样做,那么当我打印阵列时,我可能会遇到更多问题。
解决方案编辑:
function Employee(id, name){
this.id = id;
this. name = name;
} // end of employee()
function Hourly(id, name) {
Employee.call(this,id,name);
var rate = 0.0;
var hrs = 0.0;
this.setHrs = function(time){ this.hrs = time; }
this.setRate = function(pay) { this.rate = pay; }
我的主要问题可能是变量设置。我以为我理解了原型继承,但是有一些小细节导致我的代码无法运行。一旦我改变了我的代码
this.rate = rate;
this.hrs = hrs;
到
var rate = 0;
var hrs = 0;
从那里开始一帆风顺。另外,我需要调用上一个函数Employee()。我的员工变量稍后在代码中定义,但它们几乎设置相同,除了一个重要的更改...调用适当的函数。
var Employee1 = new Hourly("2262124", "Blackadder");
Employee1.setHrs(50);
Employee1.setRate(4);
Employee1.getPayCheck();
console.log(" ");
之前,我调用了Employee(),它用于为name和id分配值,但不适用于在继承链中“进一步”设置的设置速率和hrs。然后我将所有属性传递给Hourly()函数,我的属性正确地获取它们的值。
我只是想在这里发布我的解决方案给其他可能在继承方面遇到问题的人。感谢您阅读和评论!!
答案 0 :(得分:0)
setHrs()函数输入工作小时数
您应该将一些参数传递给此函数。一个setter函数将需要一些输入和" set"一些对象变量。
setHrs(hours)
您应该为您创建的每个Employee对象执行此操作。
Employee1.setHrs(50)
Employee2.setHrs(25)
我希望这会有所帮助。
修改强> 澄清我的意思:
如果您要创建一个像您已经拥有的员工对象
function Employee(id, name, hiredate, position){
this.id = id;
this.name = name;
this.hiredate = hiredate;
this.position = position;
}
然后您可以像这样实例化Employees:
var johnSmith = new Employee("6", "John Smith", "04-28-2017", "Chef")
现在,如果我们要像这样修改Employee类:
function Employee(id, name, hiredate, position){
this.id = id;
this.name = name;
this.hiredate = hiredate;
this.position = position;
this.setHrs = function (hours) = {
this.hours = hours;
}
this.setRate = function (rate) = {
this.rate = rate;
}
}
现在您可以像上面那样实例化Employee对象,但现在您可以在对象上调用这些成员函数。
johnSmith.setHours(50);
johnSmith.setRate(25);
如果你必须同时处理薪水和每小时工人,有办法处理继承,但如果你只需要处理每小时工人,这个解决方案就可以工作。
答案 1 :(得分:0)
setter函数需要获取一个参数,然后它们应该更新当前对象的属性。
this.setHrs = function(hours){
this.hrs = hours;
}
您不应该在方法中访问Employee1
和Employee2
,因为调用者可以包含其他包含这些对象的变量。他们在调用方法时使用这些变量:
Employee1.setHrs(50);
Employee2.setHrs(25);
答案 2 :(得分:0)
问题的一部分也是,而不是在console.log中的+
function Employee(id, name, hiredate, position){
this.id = id;
this. name = name;
this.hiredate = hiredate;
this.position = position;
this.rate = 0;
this.hours = 0;
}
Employee.prototype.hired = "Employed";
Employee.prototype.setHrs =function(hours){
this.hours = hours;
}
Employee.prototype.setRates = function(rates){
this.rate = rates;
}
function Hourly (employeeContext,hours, rates){
employeeContext.setRates(rates);
employeeContext.setHrs(hours);
}
var Employee1 = new Employee("4", "Blackadder",
"06-03-1902", "butler");
var Employee2 = new Employee("5", "Baldrick",
Employee1.hiredate, "who knows");
//calling Hourly
Hourly(Employee1, 4, 25);
Hourly(Employee2, 1, 50);
console.log(
"Name : " + Employee1.name +
" Hourly Rate : " + Employee1.rate+
" Hours Worked : "+ Employee1.hours
);
console.log(
"Name : " + Employee2.name +
" Hourly Rate : "+ Employee2.rate+
" Hours Worked : "+ Employee2.hours
);