javascript - Adding properties to an object using function and bracket notation -
i have assignment on basic javascript class i'm taking , can't seem work. have unit test given me:
describe('addsixthproperty', function() { it('should add food property value of bbq using bracket notation', function() { expect(objects.addsixthproperty()['food']).to.equal('bbq'); }); });
i given empty function:
// don't touch line var mysticalanimal = objects.mysticalanimal(); function addsixthelement(){ return }
so tried this:
var mysticalanimal = objects.mysticalanimal(); objects.addsixthproperty = function(){ mysticalanimal['food'] = "bbq"; return mysticalanimal["food"]; };
it doesn't work. our test page doesn't pass that. appreciated!
thanks in advance!
you're returning mysticalanimal['food']
, , test tries access ['food']
again, ends accessing 'bbq'['food']
, undefined
. need return mysticalanimal
, letter cases right. here's little proof of concept:
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'];
<p id="result" />
Comments
Post a Comment