Javascript 为构造函数中的属性赋值
我正在为JS课程做作业 指示如下: setHrs功能接受工作小时数的输入。函数应将输入保存到其实例变量名hrs。没有回报 好吧,所以我被卡住了。下面是我的代码,在这里我定义员工,给他们属性,在Employee->Employee1->Employee2之间共享这些属性 一旦我安排好了员工,我需要在每小时内创建一些功能,然后在程序中调用这些功能;getRate和getHrs 我需要为this.rate和this.hrs分配新值的指针,以下是我的代码:Javascript 为构造函数中的属性赋值,javascript,constructor,properties,prototype,Javascript,Constructor,Properties,Prototype,我正在为JS课程做作业 指示如下: setHrs功能接受工作小时数的输入。函数应将输入保存到其实例变量名hrs。没有回报 好吧,所以我被卡住了。下面是我的代码,在这里我定义员工,给他们属性,在Employee->Employee1->Employee2之间共享这些属性 一旦我安排好了员工,我需要在每小时内创建一些功能,然后在程序中调用这些功能;getRate和getHrs 我需要为this.rate和this.hrs分配新值的指针,以下是我的代码: //Prince of Wales, Bed
//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应该是数组,我可以将值推送到它们,但我尝试的一切都没有给数组添加值。另外,如果我这样做,那么当我打印阵列时,我可能会遇到更多的问题
解决方案编辑:
我的主要问题可能是变量设置。我认为我理解原型继承,但有一些小细节导致我的代码无法运行。有一次我从
this.rate = rate;
this.hrs = hrs;
到
从那以后一切都很顺利。另外,我需要调用前面的函数Employee。我的employee变量稍后在代码中定义,但它们的设置基本相同,只是有一个重要的更改。。。调用适当的函数
var Employee1 = new Hourly("2262124", "Blackadder");
Employee1.setHrs(50);
Employee1.setRate(4);
Employee1.getPayCheck();
console.log(" ");
之前,我给Employee打过电话,它负责为name和id分配值,但不负责设置继承链中较低的速率和小时数。然后,我将所有属性传递给Hourly函数,我的属性得到了相应的值
我只是想在这里发布我的解决方案,以帮助其他可能在继承实践中遇到问题的人。感谢您的阅读和评论
设置小时数功能获取工作小时数的输入
您应该向该函数传递一些参数。setter函数将接受一些输入并设置一些对象变量
setHrs(hours)
您应该为创建的每个Employee对象执行此操作
Employee1.setHrs(50)
Employee2.setHrs(25)
我希望这有帮助
编辑
为了澄清我的意思:
如果要创建一个类似于现有的Employee对象
function Employee(id, name, hiredate, position){
this.id = id;
this.name = name;
this.hiredate = hiredate;
this.position = position;
}
然后您可以像这样实例化员工:
var johnSmith = new Employee("6", "John Smith", "04-28-2017", "Chef")
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类:
var johnSmith = new Employee("6", "John Smith", "04-28-2017", "Chef")
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);
如果您必须同时处理工资和小时工,则可以处理继承,但如果您只需要处理小时工,则此解决方案将起作用。setter函数需要获取一个参数,然后它们应该更新当前对象的属性
this.setHrs = function(hours){
this.hrs = hours;
}
您不应该在方法中访问Employee1和Employee2,因为调用方可以有包含这些对象的其他变量。他们在调用方法时使用这些变量:
Employee1.setHrs(50);
Employee2.setHrs(25);
部分问题还在于console.log中的,而不是+ 函数Employeeid、名称、雇用日期、位置{ this.id=id; this.name=name; this.hiredate=hiredate; 这个位置=位置; 这个比率=0; 这1.4小时=0; } Employee.prototype.hired=Employee; Employee.prototype.setHrs=functionhours{ 这个.小时=小时; } Employee.prototype.setRates=函数速率{ 这个比率=比率; } 功能每小时雇员上下文、小时数、费率{ employeeContext.setRatesrates; employeeContext.setHrshours; } var Employee1=新员工4,Blackadder, 1902年3月6日,巴特勒; var Employee2=新员工5,Baldrick, 雇员1.雇佣,谁知道呢; //每小时打电话 小时员工1、4、25; 小时员工2,1,50; console.log 姓名:+Employee1.Name+ 小时工资:+1.5%员工工资+ 工作时间:+1.5小时 ; console.log 姓名:+Employee2.Name+ 小时工资:+2.5%员工工资+ 工作时间:+2.5小时
; 我得到的错误是setRate不是一个函数。我把你的建议放在console.log和process.stdout.write中:我用一种面向对象的方法更新了我的答案来解决你的问题。非常感谢你花时间告诉我你的意思。我确实把你的设定小时数的准确代码,设定小时数。不幸的是,setHrs,setRate需要以小时为单位。不过,您的代码确实解决了继承问题。我在函数之外的代码中添加了[Hourly.prototype=new Employee;],因为教授说我们可以继续传递原型属性。我确信问题在于继承,它只是非常混乱
现在。如果你知道一个来源,我可以得到一个更好的解释继承,这也会有所帮助。谢谢。我在打印报表之间切换