File size: 1,081 Bytes
a89f25d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// JavaScript Example: Objects and Classes
class Calculator {
    constructor() {
        this.history = [];
    }
    
    add(a, b) {
        const result = a + b;
        this.history.push(`${a} + ${b} = ${result}`);
        return result;
    }
    
    multiply(a, b) {
        const result = a * b;
        this.history.push(`${a} * ${b} = ${result}`);
        return result;
    }
    
    showHistory() {
        console.log('Calculation History:');
        this.history.forEach(entry => console.log(entry));
    }
}

// Create calculator instance
const calc = new Calculator();

// Perform calculations
console.log('10 + 5 =', calc.add(10, 5));
console.log('10 * 5 =', calc.multiply(10, 5));
console.log('7 * 8 =', calc.multiply(7, 8));

// Show history
calc.showHistory();

// Object with methods
const person = {
    name: 'Alice',
    age: 30,
    greet() {
        return `Hello, I'm ${this.name} and I'm ${this.age} years old.`;
    },
    haveBirthday() {
        this.age++;
    }
};

console.log(person.greet());
person.haveBirthday();
console.log(person.greet());