• Classes

    A class in programming is like a group of people. For example, if you have a group of friends, each person in the group is called a "friend". In programming, we call each person in the group a "class". Just like your friends might all have different names and personalities, classes can also have different names and qualities. So when you write code, you can create different classes with different names and abilities to do things.

      • Object Classes

      • class Person {
        	constructor(name, age) {
        		this.name = name;
        		this.age = age;
        	}
        
        	greet() {
        		console.log('Hello my name is ' + this.name);
        	}
        }
        
        var human = new Person('John', 36);
        
        console.log(human.name); // John
        console.log(human.age); // 36
        human.greet(); // Hello my name is John
      • Trait Classes

      • const HasFlippers = {
        	swim: function () {
        		return 'can swim';
        	}
        };
        
        const HasFeathers = {
        	fly: function () {
        		return 'can fly';
        	}
        };
        
        class Duck { }
        
        Object.assign(Duck.prototype, HasFlippers, HasFeathers);
        
        class Seal { }
        
        Object.assign(Seal.prototype, HasFlippers);
        
        var a = new Duck();
        console.log(a.swim()); // can swim
        console.log(a.fly()); // can fly
        
        var b = new Seal();
        console.log(b.swim()); // can swim
      • Interface Classes

      • Struct Classes

      • class Person {
        	constructor(name, age) {
        		this.name = name;
        		this.age = age;
        	}
        }
        
        const human = new Person('John', 36);
        
        console.log(human.name); // John
        console.log(human.age); // 36
      • Enum Classes

      • const Days = {
        	Sunday: 0,
        	Monday: 1,
        	Tuesday: 2,
        	Wednesday: 3,
        	Thursday: 4,
        	Friday: 5,
        	Saturday: 6
        };
        
        var day = 4;
        
        if (day == Days.Saturday || day == Days.Sunday) {
        	console.log('a weekend');
        } else {
        	console.log('a weekday'); // a weekday
        }