Skip to content

Instantly share code, notes, and snippets.

@wmerfalen
Created July 14, 2017 17:23
Show Gist options
  • Save wmerfalen/f7ebb24942f200365d362f4a477353ad to your computer and use it in GitHub Desktop.
Save wmerfalen/f7ebb24942f200365d362f4a477353ad to your computer and use it in GitHub Desktop.
function BaseClass() {
/* define a base class property */
this.print_debug = true;
}
BaseClass.prototype = {
ajax_call: function(func, xml, promise_callback) {
/** function definition excluded for brevity … */
this.debug('calling..' + func);
},
getClassForFunc: function(func) {
return 'Agent\\Agent';
},
debug: function(msg) {
if (this.print_debug) {
console.log(['[DEBUG:]', msg].join(''));
}
},
my_function: function(foo) {
/* In this example, we are overriding this function
* so this shall never print
*/
console.log('inside parent function');
}
};
function AgentFindUser() {
/* This is our constructor function */
}
/* We then inherit from BaseClass like this: */
AgentFindUser.prototype = Object.create(BaseClass.prototype);
AgentFindUser.prototype.my_function = function(func) {
this.print_debug = true; /* Accessing property of base class */
BaseClass.prototype.debug.call(this, func);
};
AgentFindUser.prototype.getClassForFunc = function(func) {
/* Overrides parent class method */
switch (func) {
case 'findFoo':
return 'Agent\\Foobar';
/* call the parent class function */
default:
return BaseClass.prototype.getClassForFunc.call(this, func);
}
};
AgentFindUser.prototype.constructor = AgentFindUser;
var a = new AgentFindUser();
a.my_function('findFoo'); //Prints [DEBUG]:findFoo
console.log(a.getClassForFunc('findFoo')); //Prints Agent\Foobar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment