٦.١ الأصناف¶
أساسيات الأصناف¶
التمرين 1: أنشئ بانيًا لكائنات المستخدمين.
/*
let user = {
firstName: null,
lastName: null,
email: null,
username: null
};
*/
function UserConstr(fn, ln, e, un){
this.firstName = fn;
this.lastName = ln;
this.email = e;
this.username = un;
}
let user1 = new UserConstr("Nawras", "Ali", "nawras@mail.com", "nawras77");
let user2 = new UserConstr("Jad", "Fakhri", "jad@mail.com", "fakhri5");
console.log(user1);
console.log(user2);
النتيجة كما يلي:
UserConst {firstName: "Nawras", lastName: "Ali", email: "nawras@mail.com", username: "nawras77"}
UserConst {firstName: "Jad", lastName: "Fakhri", email: "jad@mail.com", username: "fakhri5"}
التمرين 2: تأمّل الشيفرة التالية.
class User{
/* Constructor */
constructor(fn, ln, e, un){
this.firstName = fn;
this.lastName = ln;
this.email = e;
this.username = un;
}
}
let user1 = new User("Nawras", "Ali", "nawras@mail.com", "nawras77");
let user2 = new User("Jad", "Fakhri", "jad@mail.com", "fakhri5");
console.log(user1);
console.log(user2);
النتيجة كما يلي:
User {firstName: "Nawras", lastName: "Ali", email: "nawras@mail.com", username: "nawras77"}
User {firstName: "Jad", lastName: "Fakhri", email: "jad@mail.com", username: "fakhri5"}
التمرين 3: تأمّل الشيفرة التالية.
/*
let laptop = {
processor: null,
ram: null,
storage: null
}
*/
class Laptop{
/* Constructor */
constructor(proc, ram, stg){
this.processor = proc;
this.ram = ram;
this.storage = stg;
}
}
let laptop1 = new Laptop("i5", "2GB", "0.5TB");
let laptop2 = new Laptop("i3", "2GB", "1TB");
console.log(laptop1);
console.log(laptop2);
النتيجة كما يلي:
Laptop {processor: "i5", ram: "2GB", storage: "0.5TB"}
Laptop {processor: "i3", ram: "2GB", storage: "1TB"}
التمرين 4: أنشئ صنفًا لكائنات الملاحم.
/*
let epic = {
name: null;
author: null;
language: null;
}
*/
class Epic{
/*Constructor*/
constructor(name, author, lang){
this.name = name;
this.author = author;
this.language = lang;
}
}
let epic1 = new Epic("Mahabharata", "Vyasa", "Sanskrit");
let epic2 = new Epic("Ramayana", "Valmiki", "Sanskrit");
console.log(epic1);
console.log(epic2);
النتيجة كما يلي:
Epic {name: "Mahabharata", author: "Vyasa", language: "Sanskrit"}
Epic {name: "Ramayana", author: "Valmiki", language: "Sanskrit"}
التمرين 5: أنشئ صنفًا لكائنات الفيديو.
/*
let video = {
title: null,
length: null,
host: null,
language: null,
category: null
}
*/
class Video{
/*Constructor*/
constructor(title, len, h, lang, cat){
this.title = title;
this.length = len;
this.host = h;
this.language = lang;
this.category = cat;
}
}
let video1 = new Video("Create a WordPress Theme", "30min", "Nawras Ali", "Arabic", "Education");
console.log(video1);
النتيجة هي: Video {title: "Create a WordPress Theme", length: "30min", host: "Nawras Ali", language: "Arabic", category: "Education"}
التمرين 6: أنشئ صنفًا لكائنات اللغات.
/*
let language = {
name: null,
os: null,
extension: null,
stableRelease: null
}
*/
class Language{
/* Constructor */
constructor(name, os, ext, stableRel){
this.name = name;
this.os = os;
this.extension = ext;
this.stableRelease = stableRel;
}
}
let lang1 = new Language("Python", "Linux, macOS, and Windows", "py", "3.8.3");
let lang2 = new Language("PHP", "Unix-like, Windows", "php", "7.4.10");
console.log(lang1);
console.log(lang2);
النتيجة كما يلي:
Language {name: "Python", os: "Linux, macOS, and Windows", extension: "py", stableRelease: "3.8.3"}
Language {name: "PHP", os: "Unix-like, Windows", extension: "php", stableRelease: "7.4.10"}
التمرين 7: أنشئ صنفًا لكائنات الكتب.
/*
let book = {
weight: null,
pages: null
}
*/
class Book{
/* Constructor */
constructor(weight, pages){
this.weight = weight;
this.pages = pages;
}
}
let book1 = new Book(0.2, 200);
console.log(book1);
النتيجة هي: Book {weight: 0.2, pages: 200}
التمرين 8: أنشئ صنفًا لكائنات الهواتف.
/*
let phone = {
os: null,
ram: null,
storage: null
}
*/
class Phone{
/* Constructor */
constructor(os, ram, stg){
this.os = os;
this.ram = ram;
this.storage = stg;
}
}
let phone1 = new Phone("Android", "2GB", "36GB");
console.log(phone1);
النتيجة هي: Phone {os: "Android", ram: "2GB", storage: "36GB"}
التمرين 9: أنشئ صنفًا لكائنات أقلام التحديد.
/*
let markerPen = {
color: null,
price: null
}
*/
class MarkerPen{
/* Constructor */
constructor(color, price){
this.color = color;
this.price = price;
}
}
let markerPen1 = new MarkerPen("red", "2USD");
let markerPen2 = new MarkerPen("Blue", "2.2USD");
console.log(markerPen1);
console.log(markerPen2);
النتيجة كما يلي:
MarkerPen {color: "red", price: "2USD"}
MarkerPen {color: "Blue", price: "2.2USD"}
التمرين 10: أنشئ صنفًا لكائنات أقسام الدورة.
/*
let section= {
title: null,
description: null
}
*/
class Section{
/* Constructor */
constructor(title, desc){
this.title = title;
this.description = desc;
}
}
let section1 = new Section("JavaScript Basics", "Learn the basics of JavaScript in such a simple and easy way.");
let section2 = new Section("JavaScript Functions", "Introduction to JavaScript functions and closures.");
let section3 = new Section("JavaScript Arrays", "Learn about JavaScript arrays and array helpers.");
console.log(section1);
console.log(section2);
console.log(section3);
النتيجة كما يلي:
Section {title: "JavaScript Basics", description: "Learn the basics of JavaScript in such a simple and easy way."}
Section {title: "JavaScript Functions", description: "Introduction to JavaScript functions and closures."}
Section {title: "JavaScript Arrays", description: "Learn about JavaScript arrays and array helpers."}
التمرين 11: أنشئ صنفًا لكائنات الأغاني.
/*
let song = {
title: null,
artist: null,
album: null
}
*/
class Song{
/* Constructor */
constructor(title, artist, album){
this.title = title;
this.artist = artist;
this.album = album
}
}
let song1 = new Song("Orobroy", "Dorantes", "PASSO A DUE");
console.log(song1);
النتيجة هي: Song {title: "Orobroy", artist: "Dorantes", album: "PASSO A DUE"}
الأصناف — ١
الصنف قالب لإنشاء كائنات، ويحدّد الخصائص التي تصفها.
طرائق الصنف¶
التمرين 12: تأمّل الشيفرة التالية.
function UserConstr(fn, ln, e, un){
this.firstName = fn;
this.lastName = ln;
this.email = e;
this.username = un;
this.fullName = function(){
return this.firstName + " " + this.lastName;
};
}
let user1 = new UserConstr("Nawras", "Ali", "nawras@mail.com", "nawras77");
let user2 = new UserConstr("Jad", "Fakhri", "jad@mail.com", "fakhri5");
console.log(user1.fullName());
console.log(user2.fullName());
النتيجة كما يلي:
Nawras Ali
Jad Fakhri
التمرين 13: تأمّل الشيفرة التالية.
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;
}
}
let user1 = new User("Nawras", "Ali", "nawras@mail.com", "nawras77");
let user2 = new User("Jad", "Fakhri", "jad@mail.com", "fakhri5");
console.log(user1.fullName());
console.log(user2.fullName());
النتيجة كما يلي:
Nawras Ali
Jad Fakhri
التمرين 14: تأمّل الشيفرة التالية.
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;
}
}
let laptop1 = new Laptop("i5", 2, 4, "0.5TB");
let laptop2 = new Laptop("i3", 2, 8, "1TB");
console.log(laptop1.totalRam());
console.log(laptop2.totalRam());
النتيجة كما يلي:
6
10
التمرين 15: أضف مصفوفة info طريقةً إلى الصنف التالي.
/*
let epic = {
name: null;
author: null;
language: null;
info: function(){
return `${this.name} was written by ${this.author} in the ${this.language} language`;
}
}
*/
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`;
}
}
let epic1 = new Epic("Mahabharata", "Vyasa", "Sanskrit");
let epic2 = new Epic("Ramayana", "Valmiki", "Sanskrit");
console.log(epic1.info());
console.log(epic2.info());
النتيجة كما يلي:
Mahabharata was written by Vyasa in the Sanskrit language
Ramayana was written by Valmiki in the Sanskrit language
التمرين 16: أضف مصفوفة lengthInMinutes طريقةً إلى الصنف التالي.
/*
let video = {
title: null,
length: null,
lengthInMinutes: function(){
return this.length / 60;
}
}
*/
class Video{
/*Constructor*/
constructor(title, len, h, lang, cat){
this.title = title;
this.length = len;
}
/* Methods */
lengthInMinutes(){
return this.length / 60;
}
}
let video1 = new Video("Create a WordPress Theme", "3000");
console.log(video1.lengthInMinutes());
النتيجة هي: 50
التمرين 17: أضف مصفوفة lengthInHours طريقةً إلى الصنف التالي.
/*
let video = {
title: null,
length: null,
lengthInMinutes: function(){
return this.length / 60;
},
lengthInHours: function(){
return this.length / 60 / 60;
},
}
*/
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;
}
}
let video1 = new Video("Create a WordPress Theme", "3000");
console.log(video1.lengthInHours());
النتيجة هي: 0.83
التمرين 18: أضف مصفوفة tenItemsPrice طريقةً إلى الصنف التالي.
/*
let markerPen = {
color: null,
price: null,
tenItemsPrice: function(){
return this.price * 10 - this.price;
}
}
*/
class MarkerPen{
/* Constructor */
constructor(color, price){
this.color = color;
this.price = price;
}
/*Methods*/
tenItemsPrice(){
return this.price * 10 - this.price;
}
}
let markerPen1 = new MarkerPen("red", 2);
let markerPen2 = new MarkerPen("Blue", 2.2);
console.log(markerPen1.tenItemsPrice());
console.log(markerPen2.tenItemsPrice());
النتيجة كما يلي:
18
19.8
التمرين 19: أضف مصفوفة twentyItemsPrice طريقةً إلى الصنف التالي.
/*
let markerPen = {
color: null,
price: null,
tenItemsPrice: function(){
return this.price * 10 - this.price;
},
twentyItemsPrice: function(){
return (this.price * 20) - (this.price * 2);
}
}
*/
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);
}
}
let markerPen1 = new MarkerPen("red", 2);
let markerPen2 = new MarkerPen("Blue", 2.2);
console.log(markerPen1.twentyItemsPrice());
console.log(markerPen2.twentyItemsPrice());
النتيجة كما يلي:
36
39.6
التمرين 20: أضف مصفوفة fiftyItemsPrice طريقةً إلى الصنف التالي.
/*
let markerPen = {
color: null,
price: null,
tenItemsPrice: function(){
return this.price * 10 - this.price;
},
twentyItemsPrice: function(){
return (this.price * 20) - (this.price * 2);
},
fiftyItemsPrice: function(){
return (this.price * 50) - 15;
}
}
*/
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;
}
}
let markerPen1 = new MarkerPen("red", 2);
let markerPen2 = new MarkerPen("Blue", 2.2);
console.log(markerPen1.fiftyItemsPrice());
console.log(markerPen2.fiftyItemsPrice());
النتيجة كما يلي:
85
95
الأصناف — ٢
الصنف قالب لإنشاء كائنات، ويجمع الخصائص والطرائق التي تصفها.