3.9 KiB
3.9 KiB
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
56533eb9ac21ba0edf2244c9 | Accessing Object Properties with Variables | 1 | الوصول إلى خصائص الكائن مع المتغيرات |
Description
var dogs = {هناك طريقة أخرى لاستخدام هذا المفهوم وهي عندما يتم جمع اسم المنشأة ديناميكيًا أثناء تنفيذ البرنامج ، كما يلي:
Fido: "Mutt"، Hunter: "Doberman"، Snoopie: "Beagle"
}؛
var myDog = "Hunter" ؛
var myBreed = dogs [myDog]؛
console.log (myBreed)؛ // "دوبيرمان"
var someObj = {لاحظ أننا لا نستخدم علامات اقتباس حول اسم المتغير عند استخدامه للوصول إلى الخاصية لأننا نستخدم قيمة المتغير ، وليس الاسم .
propName: "John"
}؛
وظيفة 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> رقمًا
testString: 'assert(typeof playerNumber === "number", "<code>playerNumber</code> should be a number");'
- text: يجب أن يكون <code>player</code> المتغير عبارة عن سلسلة
testString: 'assert(typeof player === "string", "The variable <code>player</code> should be a string");'
- text: يجب أن تكون قيمة <code>player</code> "مونتانا"
testString: 'assert(player === "Montana", "The value of <code>player</code> should be "Montana"");'
- text: يجب عليك استخدام تدوين قوس للوصول إلى <code>testObj</code>
testString: 'assert(/testObj\s*?\[.*?\]/.test(code),"You should use bracket notation to access <code>testObj</code>");'
- text: يجب عدم تعيين قيمة <code>Montana</code> إلى <code>player</code> المتغير مباشرةً.
testString: 'assert(!code.match(/player\s*=\s*"|\"\s*Montana\s*"|\"\s*;/gi),"You should not assign the value <code>Montana</code> to the variable <code>player</code> directly.");'
- text: يجب أن تستخدم المتغير <code>playerNumber</code> في تدوين <code>playerNumber</code>
testString: 'assert(/testObj\s*?\[\s*playerNumber\s*\]/.test(code),"You should be using the variable <code>playerNumber</code> in your bracket notation");'
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 Test
console.info('after the test');
Solution
// solution required