ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Classes and subclasses
    DynamicPL/Javascript 2019. 10. 23. 12:42

    1. Overview

    Class is added in ES6 and doesn't add anything new to the language, but just synthetic sugar over the way we do prototypal inheritance in Javascript. Classes simply make it easier to implement inheritance and to create objects based on blueprints which are called function constructors in ES5.

    2. Previous way to inherit methods

    var Person5 = function(name, yearOfBirth, job) {
        this.name = name;
        this.yearOfBirth = yearOfBirth;
        this.job = job;
    }
    
    Person5.prototype.calculateAge = function() {
        var age = new Date().getFullYear() - this.yearOfBirth;
        console.log(age);
    }
    
    var Athlete5 = function(name, yearOfBirth, job, olymicGames, medals) {
        Person5.call(this, name, yearOfBirth, job);
        this.olymicGames = olymicGames;
        this.medals = medals;
    }
    
    Athlete5.prototype = Object.create(Person5.prototype);
    
    
    Athlete5.prototype.wonMedal = function() {
        this.medals++;
        console.log(this.medals);
    }
    
    
    var johnAthlete5 = new Athlete5('John', 1990, 'swimmer', 3, 10);
    
    johnAthlete5.calculateAge();
    johnAthlete5.wonMedal();

    3. ES5 way to inherit methods

    The way that a class declaration works is that all classes have to have the constructor method which is where we define the initial properties that we want in our object.

    class Person6 {
        constructor (name, yearOfBirth, job) {
            this.name = name;
            this.yearOfBirth = yearOfBirth;
            this.job = job;
        }
        
        calculateAge() {
            var age = new Date().getFullYear - this.yearOfBirth;
            console.log(age);
        }
        
        static greeting() {
            console.log('Hey there!');
        }
    }
    
    class Athlete6 extends Person6 {
        constructor(name, yearOfBirth, job, olympicGames, medals) {
            super(name, yearOfBirth, job);
            this.olympicGames = olympicGames;
            this.medals = medals;
        }
        
        wonMedal() {
            this.medals++;
            console.log(this.medals);
        }
    }
    
    const johnAthlete6 = new Athlete6('John', 1990, 'swimmer', 3, 10);
    
    johnAthlete6.wonMedal();
    johnAthlete6.calculateAge();
    
    const john6 = new Person6('John', 1990, 'teacher');
    
    Person6.greeting();

    If we want to add a method to a class, we simply would write that method in the class. And note that we don't have any separating punctuations in class like commas or semicolons.

    3.1 Static method

    We can add static methods which are methods that are simply attached to the class but not inherited by the class instances. Class definition is under the hood of a function definition so we can attach a method to it.

    • Class definition isn't hoisted unlike function constructors so we need to first implement a class
    • We can only add methods to classes but not properties. It's okay because inheriting properties isn't best practice.
    class Foo(){
       static methodName(){
          console.log("bar")
       }
    }
    

    4. Subclasses

    //ES5
    var Person5 = function(name, yearOfBirth, job) {
        this.name = name;
        this.yearOfBirth = yearOfBirth;
        this.job = job;
    }
    
    Person5.prototype.calculateAge = function() {
        var age = new Date().getFullYear() - this.yearOfBirth;
        console.log(age);
    }
    
    var Athlete5 = function(name, yearOfBirth, job, olymicGames, medals) {
    	// this points newly created athlete object by new keyword.
        Person5.call(this, name, yearOfBirth, job);
        this.olymicGames = olymicGames;
        this.medals = medals;
    }
    
    // Object.create allow us to manually set the prototype of an object
    // and we want the prototype of the athlete to be the prototype of the person so that they becom connected.
    Athlete5.prototype = Object.create(Person5.prototype);
    
    
    Athlete5.prototype.wonMedal = function() {
        this.medals++;
        console.log(this.medals);
    }
    
    
    var johnAthlete5 = new Athlete5('John', 1990, 'swimmer', 3, 10);
    
    johnAthlete5.calculateAge();
    johnAthlete5.wonMedal();
    
    
    //ES6
    class Person6 {
        constructor (name, yearOfBirth, job) {
            this.name = name;
            this.yearOfBirth = yearOfBirth;
            this.job = job;
        }
    
        calculateAge() {
            var age = new Date().getFullYear() - this.yearOfBirth;
            console.log(age);
        }
    }
    
    class Athlete6 extends Person6 {
        constructor(name, yearOfBirth, job, olympicGames, medals) {
            super(name, yearOfBirth, job);
            this.olympicGames = olympicGames;
            this.medals = medals;
        }
        
        wonMedal() {
            this.medals++;
            console.log(this.medals);
        }
    }
    
    const johnAthlete6 = new Athlete6('John', 1990, 'swimmer', 3, 10);
    
    johnAthlete6.wonMedal();
    johnAthlete6.calculateAge();

    5. References

    https://codingheroes.io/

    https://www.youtube.com/watch?v=8gWvJx-NP-w

    'DynamicPL > Javascript' 카테고리의 다른 글

    First-Class Functions and IIFE  (0) 2019.10.24
    Primitives vs. Objects  (0) 2019.10.24
    Promises, Promisification, Async, Await, and Ajax  (0) 2019.10.23
    Event Loop, Execution Stack, Web APIs, and Message Queue  (0) 2019.10.23
    Array Operations  (0) 2019.09.22

    댓글

Designed by Tistory.