3.9 KiB
3.9 KiB
id, title, challengeType, videoUrl, forumTopicId, localeTitle
id | title | challengeType | videoUrl | forumTopicId | localeTitle |
---|---|---|---|---|---|
56533eb9ac21ba0edf2244c9 | Accessing Object Properties with Variables | 1 | https://scrimba.com/c/cnQyKur | 16165 | Доступ к свойствам объектов с переменными |
Description
var dogs = {Другим способом использования этой концепции является то, что имя свойства собирается динамически во время выполнения программы, а именно:
Фидо: «Мутт», Охотник: «Доберман», Снупи: «Бигл»,
};
var myDog = "Охотник";
var myBreed = dogs [myDog];
console.log (myBreed); // "Доберман"
var someObj = {Обратите внимание, что мы не используем кавычки вокруг имени переменной при ее использовании для доступа к свойству, потому что мы используем значение переменной, а не имя .
propName: "Джон"
};
Функция propPrefix (str) {
var s = "prop";
return s + str;
}
var someProp = propPrefix ("Name"); // someProp теперь содержит значение 'propName'
console.log (someObj [someProp]); // "Джон"
Instructions
playerNumber
для поиска игрока 16
в testObj
с использованием нотации в testObj
скобок. Затем назначьте это имя переменной player
.
Tests
tests:
- text: <code>playerNumber</code> should be a number
testString: assert(typeof playerNumber === 'number');
- text: The variable <code>player</code> should be a string
testString: assert(typeof player === 'string');
- text: The value of <code>player</code> should be "Montana"
testString: assert(player === 'Montana');
- text: You should use bracket notation to access <code>testObj</code>
testString: assert(/testObj\s*?\[.*?\]/.test(code));
- text: You should not assign the value <code>Montana</code> to the variable <code>player</code> directly.
testString: assert(!code.match(/player\s*=\s*"|\'\s*Montana\s*"|\'\s*;/gi));
- text: You should be using the variable <code>playerNumber</code> in your bracket notation
testString: assert(/testObj\s*?\[\s*playerNumber\s*\]/.test(code));
Challenge Seed
// Setup
var testObj = {
12: "Namath",
16: "Montana",
19: "Unitas"
};
// Only change code below this line;
var playerNumber; // Change this Line
var player = testObj; // Change this Line
After Tests
if(typeof player !== "undefined"){(function(v){return v;})(player);}
Solution
var testObj = {
12: "Namath",
16: "Montana",
19: "Unitas"
};
var playerNumber = 16;
var player = testObj[playerNumber];