我在一个基本的javascript课程上有一个作业,而我似乎无法让它发挥作用。我有这个单元测试给了我:
describe('AddSixthProperty', function() {
it('should add a food property with the value of bbq using bracket notation', function() {
expect(objects.addSixthProperty()['food']).to.equal('BBQ');
});
});
我获得了一个空函数:
// don't touch this line
var mysticalAnimal = objects.mysticalAnimal();
function addSixthElement(){
return
}
所以我尝试了这个:
var mysticalAnimal = objects.mysticalAnimal();
objects.addSixthProperty = function(){
mysticalAnimal['food'] = "bbq";
return mysticalAnimal["food"];
};
它不起作用。我们的测试页面没有通过。非常感谢任何帮助!
提前致谢!
答案 0 :(得分:0)
您正在返回php artisan dump-autoload
,然后测试再次尝试访问mysticalAnimal['food']
,因此最终会访问['food']
'bbq'['food']
。您只需要undefined
,并且所有的信件都是正确的。这是一个小概念证明:
return mysticalAnimal
var objects = (function() {
var animal = { mystical: true };
return {
mysticalAnimal: function() { return animal; }
};
})();
var mysticalAnimal = objects.mysticalAnimal();
objects.addSixthProperty = function(){
mysticalAnimal['food'] = "bbq";
return mysticalAnimal;
};
var capturedAnimal = objects.addSixthProperty();
document.getElementById('result').innerText = capturedAnimal['food'];
答案 1 :(得分:0)
这是功能:
var mysticalAnimal = objects.mysticalAnimal();
objects.addSixthProperty = function(){
mysticalAnimal['food'] = "BBQ";
return mysticalAnimal;
};
// Test the function
console.log(objects.addSixthProperty()['food'])