Skip to content

Instantly share code, notes, and snippets.

@TheFoot
Last active September 12, 2020 07:39
Show Gist options
  • Save TheFoot/f98f968bf582fd79fae498dfb94d4d18 to your computer and use it in GitHub Desktop.
Save TheFoot/f98f968bf582fd79fae498dfb94d4d18 to your computer and use it in GitHub Desktop.

Revisions

  1. Barry Jones revised this gist Jan 13, 2020. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion 2020-coding-standards-sample-es-class.js
    Original file line number Diff line number Diff line change
    @@ -60,6 +60,8 @@ class Human extends Being {

    // Call base constructor
    super ( name, 2 );

    this.type = 'human';

    }

    @@ -69,7 +71,7 @@ class Human extends Being {
    }

    describeMyself () {
    return `${this.sayHi ()} I have ${this.eyes} and I can${this.canJudgeDistance () ? '' : 'not'} judge distance.` +
    return `${this.sayHi ()} I am a ${this.type}, and I have ${this.eyes} - I can${this.canJudgeDistance () ? '' : 'not'} judge distance.` +
    ` For dinner, I prefer to eat ${this.favouriteMeal}.`;
    }

  2. Barry Jones revised this gist Jan 13, 2020. No changes.
  3. Barry Jones revised this gist Jan 13, 2020. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions 2020-coding-standards-sample-es-class.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,7 @@
    /**
    * Sample ES6 class
    * Sample ES6 classes
    */

    /** Implement private properties */

    /** Implement private methods using symbols*/
    const _braceFood = Symbol ( 'braceFood' );

  4. Barry Jones created this gist Jan 13, 2020.
    84 changes: 84 additions & 0 deletions 2020-coding-standards-sample-es-class.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    /**
    * Sample ES6 class
    */

    /** Implement private properties */

    /** Implement private methods using symbols*/
    const _braceFood = Symbol ( 'braceFood' );

    // Base class
    class Being {

    constructor ( name, eyes = 2 ) {

    this.type = 'being';
    this.name = name;
    this.eyes = eyes;

    // Increment static counter
    Being.count++;

    }

    // Getter/setters
    set eats ( food ) {
    this.food = food;
    }

    get favouriteMeal () {
    return `${this.name} prefers ${this[ _braceFood ] ()}.`;
    }

    // Public methods
    sayHi () {
    return `${this.name} grunts`;
    }

    canJudgeDistance () {
    return this.eyes >= 2;
    }

    // Static getter
    static get howManyBeings () {
    return Being.count;
    }

    // Private method
    [ _braceFood ] () {
    return this.food || 'nothing';
    }

    }

    // Static Being properties
    Being.count = 0;

    // Human class - extending the base Being class
    class Human extends Being {

    /** Class constructor */
    constructor ( name ) {

    // Call base constructor
    super ( name, 2 );

    }

    // Override method
    sayHi () {
    return `${super.sayHi ()} and says hi!`;
    }

    describeMyself () {
    return `${this.sayHi ()} I have ${this.eyes} and I can${this.canJudgeDistance () ? '' : 'not'} judge distance.` +
    ` For dinner, I prefer to eat ${this.favouriteMeal}.`;
    }

    }

    // Export all classes
    module.exports = {
    Being,
    Human
    };