0

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 ?

3
  • If it's a global function, use window[name] Commented Mar 24, 2020 at 22:08
  • Where is that function declared? Commented Mar 24, 2020 at 22:09
  • Function could be load within this function using Jquery $.getScript. But window[name] is 'undefined', even after loading using 'getScript' Commented Mar 24, 2020 at 22:18

3 Answers 3

0

You could do typeof eval(name) === function or if the function is a global, typeof window[name] === function

Demo:

(function() {
    function test() {}
    (function(name) {
        console.log(typeof eval(name) === 'function');
    })('test');
})();

Sign up to request clarification or add additional context in comments.

4 Comments

As I said, eval() is not an alternative
Ok I read too fast, then it's not possible unless it's a global function, or unless it's attached to a particular object, is it?
I really do not know, I dynamically load script with Jquery getScript. This script contains classes or Functions. To prevent re-declare a function, this if need to evaluate and prevent script to load again.
Ok then the function you're looking for must be global. Did you try typeof window[name] === function?
0

Quick and dirty:

if ( typeof name === 'string' && typeof eval(name) === 'function' ) return dfd.promise().resolve(name);

since you probably want to double-check that name is actually a before passing it to eval. You probably also want to further validate it if it's coming from user input as you could be opening yourself up to script injections.

Comments

0

Hope this could helps anyone;

const getFunc = (s) => {
   const a = s.split(".");
   let obj = window, i = 0;
   while (i < a.length && (obj = obj[a[i++]]) !== undefined);
   if (typeof obj === "function") return obj;
};

console.log(getFunc("Infinity.isFloat"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.