3 回答
TA贡献1848条经验 获得超2个赞
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js"></script>
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script>
// JS with example of setting experience value being set third Example
// JavaScript
var Employee = Backbone.Model.extend({
//Write your code here
default: {
employee_id: 1111,
name: 'Sarah Roe',
year_of_joining: 1999,
address: 'ABC Street',
experience: null
},
getExperience: function(year_of_joining) {
var current_date = new Date();
var current_year = current_date.getFullYear();
var calculated_exp = (current_year) - (year_of_joining);
this.set({
experience: calculated_exp
});
return calculated_exp;
}
});
var employee = new Employee({
employee_id: 721,
name: "Shrikrishna",
year_of_joining: 1999,
experience: 22
});
//please add properties and values of Employee model
//employee.set({'experience': employee.get('year_of_joining')}) ;
alert("Experience of " + employee.get("name") + " is " + employee.getExperience(employee.get('year_of_joining')) + " years");
</script>
<div id="container">Loading...</div>
TA贡献1788条经验 获得超4个赞
这是backbonejs中员工模型自定义方法的正确答案:
var Employee = Backbone.Model.extend({
//Write your code here
getExperience: function(year_of_joining) {
var current_date = new Date();
var current_year = current_date.getFullYear();
var calculated_exp = (current_year) - (year_of_joining);
this.set({
experience: calculated_exp
});
return calculated_exp;
}
});
var employee = new Employee({
employee_id:1111,
name:'Sarah Roe',
year_of_joining:1999,
address:'ABC Street',
experience:0,
});
alert("Experience of "+employee.get("name")+" is "+employee.getExperience(employee.get('year_of_joining'))+" years");
<!-- Hmtl -->
<html>
<head>
</head>
<body>
<div id="app"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.13.4/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.4.1/backbone-min.js"></script>
<script type = "text/javascript" src="index.js"></script>
</html>
TA贡献1752条经验 获得超4个赞
experience
您从未为模型的属性设置新值。为了让您的测试通过,您应该使用: expect(employee.getExperience()).toBe(calculated_exp);
请注意, getExperience 函数永远不会使用 experience 参数。
或者替换:
employee.getExperience(employee.get('year_of_joining'));
和:
employee.set('experience', employee.getExperience());
添加回答
举报