انتقل إلى المحتوى

٦.٢ دوال جلب القيم والطرائق

دوال جلب القيم (Getters)

التمرين 1: تأمّل الشيفرة التالية.

class User{
  /* Constructor */
  constructor(fn, ln, e, un){
    this.firstName = fn;
    this.lastName = ln;
    this.email = e;
    this.username = un;
  }
  /*Methods*/
  fullName(){
    return this.firstName + " " + this.lastName;
  }
  /*Getters*/
  get displayFullName(){
    return this.fullName();
  }
}

let user1 = new User("Nawras", "Ali", "nawras@mail.com", "nawras77");
let user2 = new User("Jad", "Fakhri", "jad@mail.com", "fakhri5");

console.log(user1.displayFullName);
console.log(user2.displayFullName);

النتيجة كما يلي:

Nawras Ali
Jad Fakhri

التمرين 2: أنشئ دالة جلب للقيمة التي تحسبها totalRam في الصنف التالي.

class Laptop{
  /* Constructor */
  constructor(proc, ram, ar, stg){
    this.processor = proc;
    this.ram = ram;
    this.additionalRam = ar;
    this.storage = stg;
  }
  /*Methods*/
  totalRam(){
    return this.ram + this.additionalRam;
  }
  /*Getters*/
  get displayTotalRam(){
    return this.totalRam();
  }
}

let laptop1 = new Laptop("i5", 2, 4, "0.5TB");
let laptop2 = new Laptop("i3", 2, 8, "1TB");

console.log(laptop1.displayTotalRam);
console.log(laptop2.displayTotalRam);

النتيجة كما يلي:

6
10

التمرين 3: أنشئ دالة جلب لـ info في الصنف التالي.

class Epic{
  /*Constructor*/
  constructor(name, author, lang){
    this.name = name;
    this.author = author;
    this.language = lang;
  }
  /*Methods*/
  info(){
    return `${this.name} was written by ${this.author} in the ${this.language} language`;
  }
  /**Getters**/
  get displayInfo(){
    return this.info();
  }
}

let epic1 = new Epic("Mahabharata", "Vyasa", "Sanskrit");
let epic2 = new Epic("Ramayana", "Valmiki", "Sanskrit");

console.log(epic1.displayInfo);
console.log(epic2.displayInfo);

النتيجة كما يلي:

Mahabharata was written by Vyasa in the Sanskrit language
Ramayana was written by Valmiki in the Sanskrit language

التمرين 4: أنشئ دالة جلب للقيمة التي تحسبها lengthInMinutes في الصنف التالي.

class Video{
  /*Constructor*/
  constructor(title, len, h, lang, cat){
    this.title = title;
    this.length = len;
  }  
  /* Methods */
  lengthInMinutes(){
    return this.length / 60;
  }
  /*Getters*/
  get showLengthInMinutes(){
    return this.lengthInMinutes();
  }
}

let video1 = new Video("Create a WordPress Theme", "3000");

console.log(video1.showLengthInMinutes);

النتيجة هي: 50

التمرين 5: أنشئ دالة جلب للقيمة التي تحسبها lengthInHours في الصنف التالي.

class Video{
  /*Constructor*/
  constructor(title, len, h, lang, cat){
    this.title = title;
    this.length = len;
  }  
  /* Methods */
  lengthInMinutes(){
    return this.length / 60;
  }
  lengthInHours(){
    return this.length / 60 / 60;
  }
  /*Getters*/
  get showLengthInMinutes(){
    return this.lengthInMinutes();
  }
  get showLengthInHours(){
    return this.lengthInHours();
  }
}

let video1 = new Video("Create a WordPress Theme", "3000");

console.log(video1.showLengthInHours);

النتيجة هي: 0.83

التمرين 6: أنشئ دالة جلب للقيمة التي تحسبها tenItemsPrice في الصنف التالي.

class MarkerPen{
  /* Constructor */
  constructor(color, price){
    this.color = color;
    this.price = price;
  }
  /*Methods*/
  tenItemsPrice(){
    return this.price * 10 - this.price;
  }
  /*Getters*/
  get priceOf10(){
    return this.tenItemsPrice();
  }
}

let markerPen1 = new MarkerPen("red", 2);
let markerPen2 = new MarkerPen("Blue", 2.2);

console.log(markerPen1.priceOf10);
console.log(markerPen2.priceOf10);

النتيجة كما يلي:

18
19.8

التمرين 7: أنشئ دالة جلب للقيمة التي تحسبها twentyItemsPrice في الصنف التالي.

class MarkerPen{
  /* Constructor */
  constructor(color, price){
    this.color = color;
    this.price = price;
  }
  /*Methods*/
  tenItemsPrice(){
    return this.price * 10 - this.price;
  }
  twentyItemsPrice(){
    return (this.price * 20) - (this.price * 2);
  }  
  /*Getters*/
  get priceOfT10(){
    return this.tenItemsPrice();
  }
  get priceOf20(){
    return this.twentyItemsPrice();
  }    
}

let markerPen1 = new MarkerPen("red", 2);
let markerPen2 = new MarkerPen("Blue", 2.2);

console.log(markerPen1.priceOf20);
console.log(markerPen2.priceOf20);

النتيجة كما يلي:

36
39.6

التمرين 8: أنشئ دالة جلب للقيمة التي تحسبها fiftyItemsPrice في الصنف التالي.

class MarkerPen{
  /* Constructor */
  constructor(color, price){
    this.color = color;
    this.price = price;
  }
  /*Methods*/
  tenItemsPrice(){
    return this.price * 10 - this.price;
  }
  twentyItemsPrice(){
    return (this.price * 20) - (this.price * 2);
  }  
  fiftyItemsPrice(){
    return (this.price * 50) - 15;
  }
  /*Getters*/
  get priceOfT10(){
    return this.tenItemsPrice();
  }
  get priceOf20(){
    return this.twentyItemsPrice();
  }
  get priceOf50(){
    return this.fiftyItemsPrice();
  }     
}

let markerPen1 = new MarkerPen("red", 2);
let markerPen2 = new MarkerPen("Blue", 2.2);

console.log(markerPen1.priceOf50);
console.log(markerPen2.priceOf50);

النتيجة كما يلي:

85
95

دوال جلب القيم

  A getter makes it easy to access the class methods as they were a property of the object.

تدريبات إضافية

التمرين 9: أنشئ صنفًا لكائنات الدوائر.

/*
let circle = {
  name: null,
  radius: null
}
*/

class Circle{

  constructor(n, r){
    this.name = n;
    this.radius = r;
  }

}

let circle1 = new Circle("My beautiful circle", 5);

console.log(circle1);

النتيجة هي: Circle {name: "My beautiful circle", radius: 5}

التمرين 10: ارجع إلى التمرين 9؛ أضف areaCalc طريقةً إلى الصنف. تلميح: مساحة الدائرة تساوي PI * radius ** 2.

class Circle{

  constructor(n, r){
    this.name = n;
    this.radius = r;
  }

  areaCalc(){
    const PI = 3.14;
    return PI * this.radius ** 2;
  }

}

let circle1 = new Circle("My beautiful circle", 5);

console.log(circle1.areaCalc());

النتيجة هي: 78.5

التمرين 11: ارجع إلى التمرين 10؛ أضف دالة جلب لـ areaCalc .

class Circle{

  constructor(n, r){
    this.name = n;
    this.radius = r;
  }

  areaCalc(){
    const PI = 3.14;
    return PI * this.radius ** 2;
  }

  get area(){
    return this.areaCalc();
  }

}

let circle1 = new Circle("My beautiful circle", 5);

console.log(circle1.area);

النتيجة هي: 78.5

التمرين 12: ارجع إلى التمرين 11؛ أضف circumferenceCalc طريقةً إلى الصنف. تلميح: مساحة الدائرة تساوي 2 * PI * radius.

class Circle{

  constructor(n, r){
    this.name = n;
    this.radius = r;
  }

  areaCalc(){
    const PI = 3.14;
    return PI * this.radius ** 2;
  }
  circumferenceCalc(){
    const PI = 3.14;
    return 2 * PI * this.radius;
  }

  get area(){
    return this.areaCalc();
  }

}

let circle1 = new Circle("My beautiful circle", 5);

console.log(circle1.circumferenceCalc());

النتيجة هي: 31.4

التمرين 13: ارجع إلى التمرين 12؛ أضف دالة جلب لـ circumferenceCalc .

class Circle{

  constructor(n, r){
    this.name = n;
    this.radius = r;
  }

  areaCalc(){
    const PI = 3.14;
    return PI * this.radius ** 2;
  }
  circumferenceCalc(){
    const PI = 3.14;
    return 2 * PI * this.radius;
  }

  get area(){
    return this.areaCalc();
  }
  get circumference(){
    return this.circumferenceCalc();
  }

}

let circle1 = new Circle("My beautiful circle", 5);

console.log(circle1.circumference);

النتيجة هي: 31.4

التمرين 14: أنشئ صنفًا يولّد website ؟

/*
let website = {
  name: null,
  noOfPages: null,
  averagePageSize: null,
  noOfMonthlyVisits: null
}
*/

class Website{

  constructor(n, p, s, v){
    this.name = n;
    this.noOfPages = p;
    this.averagePageSize = s;
    this.noOfMonthlyVisits = v;
  }
}

let website1 = new Website("Learn With Naw", 5, 2, 1000);

console.log(website1);

النتيجة هي: Website {name: "Learn With Naw", noOfPages: 5, averagePageSize: 2, noOfMonthlyVisits: 1000}

التمرين 15: ارجع إلى التمرين 14؛ أضف monthlyBandwidthCalc طريقةً إلى الصنف. تلميح: استهلاك نقل البيانات الشهري يساوي website average size * number of pages * number of monthly visits.

class Website{

  constructor(n, p, s, v){
    this.name = n;
    this.noOfPages = p;
    this.averagePageSize = s;
    this.noOfMonthlyVisits = v;
  }

  monthlyBandwidthCalc(){
    return this.averagePageSize * this.noOfMonthlyVisits * this.noOfPages;
  }
}

let website1 = new Website("Learn With Naw", 5, 2, 1000);

console.log(website1.monthlyBandwidthCalc());

النتيجة هي: 10000

التمرين 16: ارجع إلى التمرين 15؛ أضف دالة جلب لـ monthlyBandwidthCalc .

class Website{

  constructor(n, p, s, v){
    this.name = n;
    this.noOfPages = p;
    this.averagePageSize = s;
    this.noOfMonthlyVisits = v;
  }

  monthlyBandwidthCalc(){
    return this.averagePageSize * this.noOfMonthlyVisits * this.noOfPages;
  }

  get monthlyBandwidth(){
    return this.monthlyBandwidthCalc();
  }
}

let website1 = new Website("Learn With Naw", 5, 2, 1000);

console.log(website1.monthlyBandwidth);

النتيجة هي: 10000