I've got this format
prototype method (simplified) and what I wanna do is check if char is alphabetic (case insensitive), and if it's also a function declared in the same scope. If it is, I want to call the function. So if I pass in x
it should execute the alert.
I'm gonna be using this method to format a date by the given format string. E.g. format('H:i:s')
it would check if H, i, and s is a function and call them.
How can achieve that?
I tried something based on this answer: https://stackoverflow.com/a/359910/1115367
Here's my code:
function Time() {
//initialization
}
Time.prototype = {
format: function (char) {
if (char.test(/[a-z]/i) && typeof window[char] === 'function') { //undefined
window[char]();
}
function x() {
alert('works');
}
}
};
If I pass in a value it returns: Uncaught TypeError: undefined is not a function