Paradigm 2 of 4
Object-Oriented Programming
Model your domain with objects and encapsulation. HudHudScript provides object literals, functions, and composition patterns for OOP-style programming.
🚧 Full class support coming in v0.5.0
Object Literals
Create objects with properties using object literal syntax. This is the foundation of OOP in HudHudScript:
// English - Object-Oriented Programming (OOP)
// Object-based programming patterns in HudHudScript
// ============================================
// 1. Basic Object Definition
// ============================================
object Animal {
name: "Unknown",
species: "Unknown",
age: 0,
alive: true
}
object Cat {
name: "Whiskers",
species: "Cat",
age: 3,
alive: true,
color: "orange",
indoor: true
}
print("=== Basic Objects ===");
print(Animal.name);
print(Cat.name + " (" + Cat.species + ") - " + Cat.age + " years old");
// ============================================
// 2. Factory Functions (Constructor Pattern)
// ============================================
function CreateCar(brand, model, year, color) {
return {
brand: brand,
model: model,
year: year,
color: color,
mileage: 0,
running: true
};
}
let car1 = CreateCar("Toyota", "Corolla", 2024, "white");
let car2 = CreateCar("Honda", "Civic", 2023, "black");
print("\n=== Factory Functions ===");
print(car1.brand + " " + car1.model + " - " + car1.color);
print(car2.brand + " " + car2.model + " - " + car2.color);
// ============================================
// 3. Behavior Functions (Method Pattern)
// ============================================
function car_info(car) {
return car.brand + " " + car.model + " (" + car.year + ") " + car.color;
}
function drive(car, distance) {
let new_km = car.mileage + distance;
print(car.brand + " drove " + distance + " km. Total: " + new_km + " km");
return new_km;
}
function car_age(car) {
return 2024 - car.year;
}
print("\n=== Behavior Functions ===");
print(car_info(car1));
drive(car1, 150);
drive(car1, 230);
print("Car age: " + car_age(car1) + " years");
// ============================================
// 4. Object Composition
// ============================================
function CreateAddress(city, district, neighborhood) {
return {
city: city,
district: district,
neighborhood: neighborhood
};
}
function CreatePerson(name, age, address) {
return {
name: name,
age: age,
address: address,
occupation: "Not specified"
};
}
let home_addr = CreateAddress("London", "Camden", "Primrose Hill");
let person = CreatePerson("Alice", 28, home_addr);
print("\n=== Object Composition ===");
print(person.name + " - " + person.age + " years old");
print("Address: " + person.address.neighborhood + ", " + person.address.district + "/" + person.address.city);
// ============================================
// 5. Collections and Object Arrays
// ============================================
let students = [
CreatePerson("Bob", 20, CreateAddress("New York", "Manhattan", "SoHo")),
CreatePerson("Charlie", 22, CreateAddress("San Francisco", "Mission", "Valencia")),
CreatePerson("Diana", 21, CreateAddress("Chicago", "Lincoln Park", "DePaul"))
];
print("\n=== Object Collections ===");
let idx = 0;
while (idx < 3) {
let student = students[idx];
print(student.name + " - " + student.address.city);
idx = idx + 1;
}
// ============================================
// 6. Business Logic Object (Shopping Cart)
// ============================================
function CreateCartItem(product, price, quantity) {
return {
product: product,
price: price,
quantity: quantity
};
}
function item_total(item) {
return item.price * item.quantity;
}
let cart = [
CreateCartItem("Laptop", 1200, 1),
CreateCartItem("Mouse", 25, 2),
CreateCartItem("Keyboard", 75, 1)
];
print("\n=== Shopping Cart ===");
let grand_total = 0;
let s = 0;
while (s < 3) {
let item = cart[s];
let total = item_total(item);
print(item.product + " x" + item.quantity + " = $" + total);
grand_total = grand_total + total;
s = s + 1;
}
print("Grand Total: $" + grand_total);
Properties
Define key-value pairs with any data type
Access
Use dot notation: object.property
Entity System
Model domain entities with objects and functions. Real example from the samples:
// English - Entity Concepts
let user = {
id: 1,
name: "Alice",
email: "alice@example.com",
role: "developer"
};
let product = {
id: 101,
name: "HudHudScript IDE",
version: "2.0",
price: 49
};
let order = {
id: 1001,
user_id: 1,
product_id: 101,
quantity: 2,
status: "confirmed"
};
function entity_info(entity) {
return "Entity #" + entity.id + ": " + entity.name;
}
function calculate_total(order, price) {
return order.quantity * price;
}
print("=== Entity System ===");
print(entity_info(user));
print(entity_info(product));
let total = calculate_total(order, product.price);
print("Order #" + order.id + " total: " + total);
print("Status: " + order.status);
Pattern: Objects represent entities, functions operate on them. This is prototype-based OOP without class syntax.
Functions as Methods
Functions work with objects as their primary operands:
// English - Functions
// Function definition and usage examples
// Simple function
function greet() {
print("Hello World!");
}
// Function with parameters
function add(a, b) {
return a + b;
}
// Multi-parameter function
function calculate(num1, num2, operation) {
if (operation == "add") {
return num1 + num2;
} else if (operation == "subtract") {
return num1 - num2;
} else if (operation == "multiply") {
return num1 * num2;
} else if (operation == "divide") {
return num1 / num2;
} else {
return 0;
}
}
// Mathematical functions
function square(x) {
return x * x;
}
function cube(x) {
return x * x * x;
}
// Using functions
greet();
let result1 = add(5, 3);
print(result1);
let result2 = calculate(10, 5, "add");
print(result2);
let result3 = calculate(10, 5, "multiply");
print(result3);
let square_result = square(4);
print(square_result);
let cube_result = cube(3);
print(cube_result);
// Function composition
let total = add(square(3), cube(2));
print(total);
Coming in v0.5.0
🔒
Classes
class, constructor, this, extends keywords for true OOP
// Coming soon
class Animal {
let name: String
constructor(name: String) {
this.name = name
}
}
🔒
Inheritance
Single inheritance with super keyword support
// Coming soon
class Dog extends Animal {
constructor(name: String) {
super(name)
}
}
Multilingual Examples
🇬🇧 English
// English - Object-Oriented Programming (OOP)
// Object-based programming patterns in HudHudScript
// ============================================
// 1. Basic Object Definition
// ============================================
object Animal {
name: "Unknown",
species: "Unknown",
age: 0,
alive: true
}
object Cat {
name: "Whiskers",
species: "Cat",
age: 3,
alive: true,
color: "orange",
indoor: true
}
print("=== Basic Objects ===");
print(Animal.name);
print(Cat.name + " (" + Cat.species + ") - " + Cat.age + " years old");
// ============================================
// 2. Factory Functions (Constructor Pattern)
// ============================================
function CreateCar(brand, model, year, color) {
return {
brand: brand,
model: model,
year: year,
color: color,
mileage: 0,
running: true
};
}
let car1 = CreateCar("Toyota", "Corolla", 2024, "white");
let car2 = CreateCar("Honda", "Civic", 2023, "black");
print("\n=== Factory Functions ===");
print(car1.brand + " " + car1.model + " - " + car1.color);
print(car2.brand + " " + car2.model + " - " + car2.color);
// ============================================
// 3. Behavior Functions (Method Pattern)
// ============================================
function car_info(car) {
return car.brand + " " + car.model + " (" + car.year + ") " + car.color;
}
function drive(car, distance) {
let new_km = car.mileage + distance;
print(car.brand + " drove " + distance + " km. Total: " + new_km + " km");
return new_km;
}
function car_age(car) {
return 2024 - car.year;
}
print("\n=== Behavior Functions ===");
print(car_info(car1));
drive(car1, 150);
drive(car1, 230);
print("Car age: " + car_age(car1) + " years");
// ============================================
// 4. Object Composition
// ============================================
function CreateAddress(city, district, neighborhood) {
return {
city: city,
district: district,
neighborhood: neighborhood
};
}
function CreatePerson(name, age, address) {
return {
name: name,
age: age,
address: address,
occupation: "Not specified"
};
}
let home_addr = CreateAddress("London", "Camden", "Primrose Hill");
let person = CreatePerson("Alice", 28, home_addr);
print("\n=== Object Composition ===");
print(person.name + " - " + person.age + " years old");
print("Address: " + person.address.neighborhood + ", " + person.address.district + "/" + person.address.city);
// ============================================
// 5. Collections and Object Arrays
// ============================================
let students = [
CreatePerson("Bob", 20, CreateAddress("New York", "Manhattan", "SoHo")),
CreatePerson("Charlie", 22, CreateAddress("San Francisco", "Mission", "Valencia")),
CreatePerson("Diana", 21, CreateAddress("Chicago", "Lincoln Park", "DePaul"))
];
print("\n=== Object Collections ===");
let idx = 0;
while (idx < 3) {
let student = students[idx];
print(student.name + " - " + student.address.city);
idx = idx + 1;
}
// ============================================
// 6. Business Logic Object (Shopping Cart)
// ============================================
function CreateCartItem(product, price, quantity) {
return {
product: product,
price: price,
quantity: quantity
};
}
function item_total(item) {
return item.price * item.quantity;
}
let cart = [
CreateCartItem("Laptop", 1200, 1),
CreateCartItem("Mouse", 25, 2),
CreateCartItem("Keyboard", 75, 1)
];
print("\n=== Shopping Cart ===");
let grand_total = 0;
let s = 0;
while (s < 3) {
let item = cart[s];
let total = item_total(item);
print(item.product + " x" + item.quantity + " = $" + total);
grand_total = grand_total + total;
s = s + 1;
}
print("Grand Total: $" + grand_total);
🇹🇷 Turkish
// Türkçe - Nesne Yönelimli Programlama (OOP)
// HudHudScript'te nesne tabanlı programlama kalıpları
// ============================================
// 1. Temel Nesne Tanımlama
// ============================================
nesne Hayvan {
isim: "Bilinmiyor",
tür: "Bilinmiyor",
yaş: 0,
canlı: doğru
}
nesne Kedi {
isim: "Tekir",
tür: "Kedi",
yaş: 3,
canlı: doğru,
renk: "turuncu",
iç_mekan: doğru
}
yaz("=== Temel Nesneler ===");
yaz(Hayvan.isim);
yaz(Kedi.isim + " (" + Kedi.tür + ") - " + Kedi.yaş + " yaşında");
// ============================================
// 2. Fabrika Fonksiyonları (Constructor Pattern)
// ============================================
işlev ArabaOluştur(marka, model, yıl, renk) {
dön {
marka: marka,
model: model,
yıl: yıl,
renk: renk,
kilometre: 0,
çalışıyor: doğru
};
}
değişken araba1 = ArabaOluştur("Toyota", "Corolla", 2024, "beyaz");
değişken araba2 = ArabaOluştur("Honda", "Civic", 2023, "siyah");
yaz("\n=== Fabrika Fonksiyonu ===");
yaz(araba1.marka + " " + araba1.model + " - " + araba1.renk);
yaz(araba2.marka + " " + araba2.model + " - " + araba2.renk);
// ============================================
// 3. Davranış Fonksiyonları (Method Pattern)
// ============================================
işlev araba_bilgisi(araba) {
dön araba.marka + " " + araba.model + " (" + araba.yıl + ") " + araba.renk;
}
işlev araba_sür(araba, mesafe) {
değişken yeni_km = araba.kilometre + mesafe;
yaz(araba.marka + " " + mesafe + " km yol aldı. Toplam: " + yeni_km + " km");
dön yeni_km;
}
işlev araba_yaşı(araba) {
dön 2024 - araba.yıl;
}
yaz("\n=== Davranış Fonksiyonları ===");
yaz(araba_bilgisi(araba1));
araba_sür(araba1, 150);
araba_sür(araba1, 230);
yaz("Araç yaşı: " + araba_yaşı(araba1) + " yıl");
// ============================================
// 4. Nesne Kompozisyonu
// ============================================
işlev AdresOluştur(şehir, ilçe, mahalle) {
dön {
şehir: şehir,
ilçe: ilçe,
mahalle: mahalle
};
}
işlev KişiOluştur(isim, yaş, adres) {
dön {
isim: isim,
yaş: yaş,
adres: adres,
meslek: "Belirtilmemiş"
};
}
değişken ev_adresi = AdresOluştur("İstanbul", "Kadıköy", "Moda");
değişken kişi = KişiOluştur("Davut", 28, ev_adresi);
yaz("\n=== Nesne Kompozisyonu ===");
yaz(kişi.isim + " - " + kişi.yaş + " yaşında");
yaz("Adres: " + kişi.adres.mahalle + ", " + kişi.adres.ilçe + "/" + kişi.adres.şehir);
// ============================================
// 5. Koleksiyon ve Nesne Dizileri
// ============================================
değişken öğrenciler = [
KişiOluştur("Elif", 20, AdresOluştur("Ankara", "Çankaya", "Kızılay")),
KişiOluştur("Mehmet", 22, AdresOluştur("İzmir", "Konak", "Alsancak")),
KişiOluştur("Zeynep", 21, AdresOluştur("Bursa", "Nilüfer", "Özlüce"))
];
yaz("\n=== Nesne Koleksiyonları ===");
değişken idx = 0;
iken (idx < 3) {
değişken öğrenci = öğrenciler[idx];
yaz(öğrenci.isim + " - " + öğrenci.adres.şehir);
idx = idx + 1;
}
// ============================================
// 6. Hesaplama Nesnesi (Alışveriş Sepeti)
// ============================================
işlev SepetÖğesiOluştur(ürün, fiyat, adet) {
dön {
ürün: ürün,
fiyat: fiyat,
adet: adet
};
}
işlev toplam_fiyat(öğe) {
dön öğe.fiyat * öğe.adet;
}
değişken sepet = [
SepetÖğesiOluştur("Laptop", 25000, 1),
SepetÖğesiOluştur("Mouse", 500, 2),
SepetÖğesiOluştur("Klavye", 1200, 1)
];
yaz("\n=== Alışveriş Sepeti ===");
değişken genel_toplam = 0;
değişken s = 0;
iken (s < 3) {
değişken öğe = sepet[s];
değişken tutar = toplam_fiyat(öğe);
yaz(öğe.ürün + " x" + öğe.adet + " = " + tutar + " TL");
genel_toplam = genel_toplam + tutar;
s = s + 1;
}
yaz("Genel Toplam: " + genel_toplam + " TL");