foods对象被用于超市收银机的程序中。我们有一些设置selectedFood功能,我们想检查我们的foods对象是否存在该食物。这可能看起来像: let selectedFood = getCurrentFood(scanningItem);此代码将评估存储在
让库存=食物[selectedFood];
selectedFood变量中的值,并在foods对象中返回该键的值,如果不存在则返回undefined 。括号表示法非常有用,因为有时候对象属性在运行时之前是未知的,或者我们需要以更动态的方式访问它们。 checkInventory ,它接收一个扫描的项目作为参数。返回foods对象中的scannedItem键的当前值。您可以假设只有有效键将作为checkInventory的参数提供。 checkInventory是一个函数
testString: 'assert.strictEqual(typeof checkInventory, "function", "checkInventory is a function");'
- text: 'foods对象应该只有以下键值对: apples: 25 , oranges: 32 , plums: 28 , bananas: 13 , grapes: 35 , strawberries: 27'
testString: 'assert.deepEqual(foods, {apples: 25, oranges: 32, plums: 28, bananas: 13, grapes: 35, strawberries: 27}, "The foods object should have only the following key-value pairs: apples: 25, oranges: 32, plums: 28, bananas: 13, grapes: 35, strawberries: 27");'
- text: checkInventory("apples")应该返回25
testString: 'assert.strictEqual(checkInventory("apples"), 25, "checkInventory("apples") should return 25");'
- text: checkInventory("bananas")应该返回13
testString: 'assert.strictEqual(checkInventory("bananas"), 13, "checkInventory("bananas") should return 13");'
- text: checkInventory("strawberries")应该返回27
testString: 'assert.strictEqual(checkInventory("strawberries"), 27, "checkInventory("strawberries") should return 27");'
```