I`m trying to build an function that load script on-demand. That is my current code:
function loadScript(src, name = null)
{
var dfd = jQuery.Deferred();
if (name === null) {
name = src.split(/\\|\//); // split by folder separator
name = name[(name.length - 1)].split('.'); // catch last index & split by extension
name.splice(name.length - 1, 1) // Remove last
name = name.join('.'); // add points in the middle of file name
}
if ( typeof name === 'function' ) return dfd.promise().resolve(name);
$.getScript( src )
.done(function( script, textStatus ) {
dfd.resolve(name);
})
.fail(function( jqxhr, settings, exception ) {
dfd.reject(exception);
});
return dfd.promise();
}
My problem is on this part of code:
if ( typeof name === 'function' ) return dfd.promise().resolve(name);
Where name is a variable that contains desired function name to check, but not the real function name, causing function never evaluate as 'function'.
I tried:
typeof `${name}` // resulting a "string"
eval("typeof name === 'function'") // But my node system not accept eval due to a potentially security risk
How many alternatives that I have ?