freeCodeCamp/curriculum/challenges/arabic/02-javascript-algorithms-an.../basic-javascript/accessing-object-properties...

4.0 KiB

id title challengeType guideUrl videoUrl localeTitle
56533eb9ac21ba0edf2244c9 Accessing Object Properties with Variables 1 https://arabic.freecodecamp.org/guide/certificates/accessing-objects-properties-with-variables الوصول إلى خصائص الكائن مع المتغيرات

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> &quot;مونتانا&quot;
    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