Created
July 14, 2017 17:22
-
-
Save wmerfalen/8ddfd8e1225c93bba0ff5e369db2219d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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