0

I want to used objects to store function name and another properties which i will used it to load jS script by Jquery getScript. I have used conditional by inAarray to check if an object is existing in this array I will call that scrip and execute that function (func) but I still have stack to convert that object value to a function for execution after JS scrip load within status success.

    function fetchingData(n_source_id, types, not_id) {

   var data = {
        'Loan Repayment':{
            url:'/js/load/loan/loan_repayment.js',
            func:'repayment'// name of function which I will call after JS file successfully loaded
        },
        'Reject Repayment':{
            url:'/js/load/loan/repay_change_till_acc.js',
            func:'changeTill'// name of function which I will call after JS file successfully loaded
        },
        'Issue Till': {
            url: '/js/load/issueTill.js',
            func: 'issueTill'// name of function which I will call after JS file successfully loaded
        }
    };
        $.each(data, function(inx, vals) {

            var _type = inx;
            var url = vals;

            if ($.inArray(types, [_type]) >= 0) {

                ScriptRequire([vals.url], function(status) {

                    if (status === 'success') {

                        var fun = new Function(vals.function);

                        console.log(fun(n_source_id, types));
                        return fun;
                    }
                });
            }
        });
    }
0

1 Answer 1

1

Since vals.function holds the name of the function, and the function should already be defined by the loaded script, do not use the following to get the function object:

var fun = new Function(vals.function);

Instead, use:

var fun = window[vals.function]; 
Sign up to request clarification or add additional context in comments.

3 Comments

Now I decided to used object method instead it may be better than
@HengSopheak - Do you mean the functions loaded by getScript are not global functions, but are properties of an object? Or do you mean you are no longer looking for an answer to your question?
As I have tested your code it work fine but now I used access method from object instead.

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.