Skip to content

Instantly share code, notes, and snippets.

@sandrabosk
Last active February 2, 2021 05:44
Show Gist options
  • Save sandrabosk/95d9c3679e5b9229cd7fd868b71c6bfa to your computer and use it in GitHub Desktop.
Save sandrabosk/95d9c3679e5b9229cd7fd868b71c6bfa to your computer and use it in GitHub Desktop.
// we can declare multiple objects and give them different values for the same properties,
// which means we have to repeat the same process for every next person
// Class - template for creating objects of new custom type
// In simple English, create just one object and be able to reuse it as a blueprint for all the others
// --------------------------------------------------------
// To create a class all we need is a class keyword followed by an identifier (a name we gave to the class)
// and a block of code in between the curly {} braces.
// --------------------------------------------------------
// - Camel case but the FIRST LETTER is typically uppercase (Person, Book, Author)
// - Name is typically SINGULAR (Book, not Books)
// OOP Class
class Person {
constructor (name, age, country) {
this.name = name;
this.age = age;
this.country = country;
// A method could also be defined like this
// although that would defeat the purpose
// for having a special syntax to define methods
// this.sayAge = function () {
// console.log('I am ' + this.age + ' years old');
// }
this.sayHappyBirthday = sayHappyBirthday;
}
getAge () {
console.log(`${this.name} is ${this.age} years old.`);
}
greet () {
console.log('Hello!');
}
getName () {
console.log(`My name is ${this.name}.`);
}
}
const sayHappyBirthday = function () {
console.log(`Happy Birthday ${this.name}!`);
};
sayHappyBirthday(); // Happy Birthday undefined
const anaPerson = new Person('Ana', 26, 'Brazil');
const tomasPerson = new Person('Tomas', 28, 'Argentina');
// anaPerson.greet();
// anaPerson.getName();
// anaPerson.getAge();
// anaPerson.sayHappyBirthday();
console.log('----------------------------');
// tomasPerson.greet();
// tomasPerson.getName();
// tomasPerson.getAge();
// tomasPerson.sayHappyBirthday();
console.log('----------------------------');
// CONTRUCTOR: The constructor method is a special method for creating and initializing an object created with a class.
// There can only be one special method with the name “constructor” in a class.
// In the constructor, "this" refers to the new object created.
// In simple English, the constructor is a method which is used to create the new instances,
// new objects being created based on the class.
// ------------------------------
// EXTENDS: Using the keyword extends we can add one more layer of abstraction.
// We can create a new class that will have all the attributes and methods of another class (+ probably some of their own),
// and for that we will use the keyword "extends". This is known as INHERITANCE.
class Student extends Person {
constructor (name, age, country, course) {
// `super` refers to the constructor of the parent (Person)
// with super Student gets all the attributes and methods of the Person class
super(name, age, country);
// course is a new property, only characteristic for student
this.course = course;
}
complainAboutClass () {
console.log('I wish the pace is slower...');
}
graduate (howManyProjects) {
if (howManyProjects >= 3) return { didGraudate: true, message:`${this.name} successfully graduated.` }
else return { didGraduate: false, message: `Keep grinding, my friend, you are almost there.` }
}
}
const stefanStudent = new Student('Stefan', 29, 'Montenegro', 'Web Dev');
stefanStudent.greet();
stefanStudent.getName();
stefanStudent.getAge();
stefanStudent.complainAboutClass();
stefanStudent.graduate(2).message;
// const arrayIsInstanceOfArrayClass = {} instanceof Array;
// console.log(arrayIsInstanceOfArrayClass);
console.log(`Is Stefan instance of the Person class? -> ${stefanStudent instanceof Person}`);
console.log(`Is Stefan instance of the Student class? -> ${stefanStudent instanceof Student}`);
console.log(`Is Ana instance of the Person class? -> ${anaPerson instanceof Person}`);
console.log(`Is Ana instance of the Student class? -> ${anaPerson instanceof Student}`);
// Doesnt work
// since ana is a person but not a student
// anaPerson.complainAboutClass();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment