3.2 KiB
3.2 KiB
id, title, challengeType, videoUrl, forumTopicId, localeTitle
id | title | challengeType | videoUrl | forumTopicId | localeTitle |
---|---|---|---|---|---|
56533eb9ac21ba0edf2244c8 | Accessing Object Properties with Bracket Notation | 1 | https://scrimba.com/c/cBvmEHP | 16163 | Доступ к объектным свойствам с помощью скобок |
Description
[]
). Если свойство объекта, к которому вы пытаетесь получить доступ, имеет пробел в своем имени, вам нужно будет использовать нотацию в виде скобок. Тем не менее, вы все равно можете использовать нотацию нот для объектов без пробелов. Ниже приведен пример использования обозначения скобок для чтения свойства объекта: var myObj = {Обратите внимание, что имена свойств с пробелами в них должны быть в кавычках (один или два).
«Space Name»: «Kirk»,
«Больше пространства»: «Спок»,
«NoSpace»: «USS Enterprise»
};
myObj ["Space Name"]; // Кирк
myObj ['More Space']; // Спок
myObj [ "NoSpace"]; // USS Enterprise
Instructions
"an entree"
и "the drink"
testObj
с использованием скобкой и назначьте их entreeValue
и drinkValue
соответственно.
Tests
tests:
- text: <code>entreeValue</code> should be a string
testString: assert(typeof entreeValue === 'string' );
- text: The value of <code>entreeValue</code> should be <code>"hamburger"</code>
testString: assert(entreeValue === 'hamburger' );
- text: <code>drinkValue</code> should be a string
testString: assert(typeof drinkValue === 'string' );
- text: The value of <code>drinkValue</code> should be <code>"water"</code>
testString: assert(drinkValue === 'water' );
- text: You should use bracket notation twice
testString: assert(code.match(/testObj\s*?\[('|")[^'"]+\1\]/g).length > 1);
Challenge Seed
// Setup
var testObj = {
"an entree": "hamburger",
"my side": "veggies",
"the drink": "water"
};
// Only change code below this line
var entreeValue = testObj; // Change this line
var drinkValue = testObj; // Change this line
After Tests
(function(a,b) { return "entreeValue = '" + a + "', drinkValue = '" + b + "'"; })(entreeValue,drinkValue);
Solution
var testObj = {
"an entree": "hamburger",
"my side": "veggies",
"the drink": "water"
};
var entreeValue = testObj["an entree"];
var drinkValue = testObj['the drink'];