Skip to content

Instantly share code, notes, and snippets.

@dtomasi dtomasi/Vererbung.js
Created Jan 10, 2014

Embed
What would you like to do?
Vorlage für einen Klassenaufbau in Javascript
/**
* 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.