users
对象,这可能看起来像: for(让用户在用户中){在这个语句中,我们定义了一个变量
的console.log(用户);
};
//日志:
艾伦
杰夫
莎拉
瑞安
user
,正如您所看到的,在每次迭代期间,当该语句循环遍历该对象时,该变量被重置为每个对象的键,从而导致每个用户的名称被打印到控制台。 注意: countOnline
;在此函数中使用for ... in语句循环访问users
对象中的users
并返回其online
属性设置为true
的用户数。 users
对象包含用户Jeff
和Ryan
, online
设置为true
,用户Alan
和Sarah
online
设置为false
testString: 'assert(users.Alan.online === false && users.Jeff.online === true && users.Sarah.online === false && users.Ryan.online === true, "The users
object contains users Jeff
and Ryan
with online
set to true
and users Alan
and Sarah
with online
set to false
");'
- text: 函数countOnline
返回online
属性设置为true
的用户数
testString: 'assert((function() { users.Harry = {online: true}; users.Sam = {online: true}; users.Carl = {online: true}; return countOnline(users) })() === 5, "The function countOnline
returns the number of users with the online
property set to true
");'
```