Skip to content

Instantly share code, notes, and snippets.

@dtomasi
Created January 10, 2014 10:25
Show Gist options
  • Select an option

  • Save dtomasi/c5316c76ea9fa0c68286 to your computer and use it in GitHub Desktop.

Select an option

Save dtomasi/c5316c76ea9fa0c68286 to your computer and use it in GitHub Desktop.

Revisions

  1. Dominik Tomasi created this gist Jan 10, 2014.
    109 changes: 109 additions & 0 deletions Vererbung.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,109 @@
    /**
    * Vorlage für Klassenaufbau mit Vererbung in Javascript
    * @constructor
    */

    /**
    * Define Class ParentClass
    * @constructor
    */
    function ParentClass() {

    /**
    * Define Properties
    * @type {string}
    */
    this.ExampleProperty = 'Hello'
    }

    /**
    * Define A Funktion for Class ParentClass
    * @param sayWhat
    */
    ParentClass.prototype.sayHello = function(sayWhat){
    console.log('Method sayHello() from ParentClass');
    console.log(sayWhat);
    };


    /**
    * Define a ChildClass
    * @constructor
    */
    function ChildClass() {

    /**
    * Define Child Property
    * @type {string}
    */
    this.ExampleProperty2 = 'Hello Number 2';

    /**
    * Overwrite Parent Property
    * @type {string}
    */
    this.ExampleProperty = 'say ';

    /**
    * Extend Parent Property
    * @type {string}
    */
    this.ExampleProperty = ChildClass.parent.ExampleProperty + 'Hello World';

    }

    /**
    * Define Parent for ChildClass for Inherit Methods and Properties
    * @type {ParentClass}
    */
    ChildClass.prototype = new ParentClass();

    /**
    * Set Parent For Calling Parent Methods form overwritten Methods in Child Class
    * @type {ParentClass}
    */
    ChildClass.parent = new ParentClass();

    /**
    * Reset Pointer to right Constructor
    * Because Constructor-Pointer is set to ParentClass by Inherit
    * @type {ChildClass}
    */
    ChildClass.prototype.constructor = ChildClass;

    /**
    * Overwrite Parent Method
    * @param sayWhat
    */
    ChildClass.prototype.sayHello = function(sayWhat)
    {
    console.log('called Child Method');
    ChildClass.parent.sayHello(sayWhat);
    };



    /**
    * Create a Instance of ParentClass
    * @type {ParentClass}
    */
    var parent = new ParentClass();
    // Call a Function
    parent.sayHello('Hello');

    console.log('ExampleProperty of ParentClass');
    console.log(parent.ExampleProperty);

    /**
    * Create a Instance of ChildClass
    * @type {ChildClass}
    */
    var child = new ChildClass();
    // Call the same Function with other Params
    child.sayHello('Say What?');

    console.log('ExampleProperty of ChildClass');
    console.log(child.ExampleProperty);

    console.log('ExampleProperty2 of ChildClass');
    console.log(child.ExampleProperty2);