Skip to content

Instantly share code, notes, and snippets.

@ludovicdeluna
Last active April 22, 2016 10:21
Show Gist options
  • Save ludovicdeluna/aae7cc09832b7e2914c1e48e081c6acf to your computer and use it in GitHub Desktop.
Save ludovicdeluna/aae7cc09832b7e2914c1e48e081c6acf to your computer and use it in GitHub Desktop.

Revisions

  1. ludovicdeluna revised this gist Apr 22, 2016. 1 changed file with 10 additions and 10 deletions.
    20 changes: 10 additions & 10 deletions es5.1-supermethod.js
    Original file line number Diff line number Diff line change
    @@ -7,11 +7,11 @@
    if ( !(this instanceof Feline) ) return;
    this.name = name;
    this.race = race || "Feline"
    console.log("Feline ADN new say : A new " + this.race + " is born : " + this.name);
    console.log("Feline DNA new say : A new " + this.race + " is born : " + this.name);
    };

    Feline.prototype.hello = function(){
    console.log("Feline ADN hello say : " + this.myName());
    console.log("Feline DNA hello say : " + this.myName());
    };

    Feline.prototype.myName = function(){
    @@ -39,8 +39,8 @@
    Cat.prototype.hello = function(_super){
    return function(){
    _super.apply(this);
    console.log("Cat ADN hello say: And my owner is " + this.owner);
    console.log("Cat ADN hello say: ... Yes, I inherit also of this capacity to say " + this.myName()); // Method exists only in Feline prototype
    console.log("Cat DNA hello say: And my owner is " + this.owner);
    console.log("Cat DNA hello say: ... Yes, I inherit also of this capacity to say " + this.myName()); // Method exists only in Feline prototype
    };
    }(Cat.prototype.hello); // Stick to the initial hello method through the prototype chain
    console.log(Cat);
    @@ -59,17 +59,17 @@
    Starting
    function Feline()
    --- Here: leo = new Feline("Léo");
    Feline ADN new say : A new Feline is born : Léo
    Feline DNA new say : A new Feline is born : Léo
    Object { name: "Léo", race: "Feline" }
    --- Here: leo.hello();
    Feline ADN hello say : My name is Léo
    Feline DNA hello say : My name is Léo
    function Cat()
    --- Here: kitty = new Cat("Kitty", "John");
    Feline ADN new say : A new Cat is born : Kitty
    Feline DNA new say : A new Cat is born : Kitty
    Object { name: "Kitty", race: "Cat", owner: "John" }
    --- Here: kitty.hello();
    Feline ADN hello say : My name is Kitty
    Cat ADN hello say: And my owner is John
    Cat ADN hello say: ... Yes, I inherit also of this capacity to say My name is Kitty
    Feline DNA hello say : My name is Kitty
    Cat DNA hello say: And my owner is John
    Cat DNA hello say: ... Yes, I inherit also of this capacity to say My name is Kitty
    End.
    */
  2. ludovicdeluna revised this gist Apr 22, 2016. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions es5.1-supermethod.js
    Original file line number Diff line number Diff line change
    @@ -14,9 +14,9 @@
    console.log("Feline ADN hello say : " + this.myName());
    };

    Feline.prototype.myName = function(){
    return "My name is " + this.name;
    }
    Feline.prototype.myName = function(){
    return "My name is " + this.name;
    }
    console.log(Feline);

    console.log('--- Here: leo = new Feline("Léo");');
    @@ -40,7 +40,7 @@
    return function(){
    _super.apply(this);
    console.log("Cat ADN hello say: And my owner is " + this.owner);
    console.log("Cat ADN hello say: ... Yes, I inherit also of this capacity to say " + this.myName()); // Method exists only in Feline prototype
    console.log("Cat ADN hello say: ... Yes, I inherit also of this capacity to say " + this.myName()); // Method exists only in Feline prototype
    };
    }(Cat.prototype.hello); // Stick to the initial hello method through the prototype chain
    console.log(Cat);
  3. ludovicdeluna created this gist Apr 22, 2016.
    75 changes: 75 additions & 0 deletions es5.1-supermethod.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    // How use super like in OOP with a Prototypal Language :
    !function(){
    'use strict';
    console.log("Starting");

    var Feline = function(name, race) {
    if ( !(this instanceof Feline) ) return;
    this.name = name;
    this.race = race || "Feline"
    console.log("Feline ADN new say : A new " + this.race + " is born : " + this.name);
    };

    Feline.prototype.hello = function(){
    console.log("Feline ADN hello say : " + this.myName());
    };

    Feline.prototype.myName = function(){
    return "My name is " + this.name;
    }
    console.log(Feline);

    console.log('--- Here: leo = new Feline("Léo");');
    var leo = new Feline("Léo");
    console.log(leo);
    console.log('--- Here: leo.hello();');
    leo.hello();

    var Cat = function(_super) {
    return function(name, owner) {
    if ( !(this instanceof Cat) ) return;
    _super.apply(this, [name, "Cat"]);
    this.owner = owner;
    };
    }(Feline);

    Cat.prototype = Object.create(Feline.prototype);
    Cat.prototype.constructor = Cat;

    Cat.prototype.hello = function(_super){
    return function(){
    _super.apply(this);
    console.log("Cat ADN hello say: And my owner is " + this.owner);
    console.log("Cat ADN hello say: ... Yes, I inherit also of this capacity to say " + this.myName()); // Method exists only in Feline prototype
    };
    }(Cat.prototype.hello); // Stick to the initial hello method through the prototype chain
    console.log(Cat);

    console.log('--- Here: kitty = new Cat("Kitty", "John");');
    var kitty = new Cat("Kitty", "John");
    console.log(kitty);
    console.log('--- Here: kitty.hello();');
    kitty.hello();


    console.log("End.");
    }();

    /*
    Starting
    function Feline()
    --- Here: leo = new Feline("Léo");
    Feline ADN new say : A new Feline is born : Léo
    Object { name: "Léo", race: "Feline" }
    --- Here: leo.hello();
    Feline ADN hello say : My name is Léo
    function Cat()
    --- Here: kitty = new Cat("Kitty", "John");
    Feline ADN new say : A new Cat is born : Kitty
    Object { name: "Kitty", race: "Cat", owner: "John" }
    --- Here: kitty.hello();
    Feline ADN hello say : My name is Kitty
    Cat ADN hello say: And my owner is John
    Cat ADN hello say: ... Yes, I inherit also of this capacity to say My name is Kitty
    End.
    */