Encapsulation, Functions - Function call per String

2

I have a problem checking to see if a particular function exists.

Code - Similar.

if(jQuery){
    (function(jQuery){
            jQuery.extend(jQuery.fn, {
                exemplo: function(o){

                    var chartError = false;

                    function parseNumber(value){
                        return parseFloat(value) || 0;
                    }

                    function parseString(value){
                        return ''+value+'';
                    }

                    function parseBoolean(value){
                        return (/false/i).test(value) ? false : !!value;
                    }

                    function ucfirst(str) {
                        str += '';
                        var f = str.charAt(0).toUpperCase();
                        return f + str.substr(1);
                    }

                    function inArray(needle, haystack) {
                        var length = haystack.length;
                        for(var i = 0; i < length; i++) {
                            if(haystack[i] == needle) return true;
                        }
                        return false;
                    }

                    // Defaults
                    var types = [
                       'string',
                       'number',
                       'boolean',
                       'date',
                       'datetime',
                       'timeofday',
                    ];

                    if( o.type == undefined ) o.type = 'string';
                    if( o.value == undefined ) o.value = '';


                    if(!inArray(c.type, types)){
                        chartError = 'the content of "type" is not valid';
                    }

                    if(!chartError){
                        var fn = 'parse'+ucfirst(c.type);

                        if(typeof window[fn] == 'function'){
                            o.value = window[fn](o.value);
                        }
                    }
                }
            });
    })(jQuery);
}

Situation

I'm building the function from a string var fn = 'parse'+ucfirst(c.type); and I want to first validate if the function exists if you call it to change the data type of o.value .

Search

how to execute a function when i have its name
The answer is very clear, as you can see I'm even making use of it, if(typeof window[fn] == 'function') , however there is a problem, these functions are not in the scope of window , they are encapsulated along the jQuery extension, that is the scope of them is the function that I am mounting.

Question

How to check if the function exists within the scope of the extension?

    
asked by anonymous 01.07.2015 / 15:05

1 answer

2

Solution to date:

var f = {};
f.parseNumber = function parseNumber(value){
    return parseFloat(value) || 0;
}

f.parseString = function parseString(value){
    return ''+value+'';
}

f.parseBoolean = function parseBoolean(value){
    return (/false/i).test(value) ? false : !!value;
}

f.arrayKeys = function arrayKeys(input) {
    var output = new Array();
    var counter = 0;
    for (i in input) {
        output[counter++] = i;
    } 
    return output;
}

f.inArray = function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;
}

f.ucfirst = function ucfirst(str) {
    str += '';
    var f = str.charAt(0).toUpperCase();
    return f + str.substr(1);
}

Test

var fn = 'parseNumber';
f.inArray(fn, f.arrayKeys(f)); // true
fn = 'parseDate';
f.inArray(fn, f.arrayKeys(f)); // false

Suggestion for @bfavaretto

var fn = 'parseNumber';
typeof f[fn] == 'function' // true

var fn = 'parseDate';
typeof f[fn] == 'function' // false
    
01.07.2015 / 15:43