admin管理员组

文章数量:1379645

I'm a JS noob, so apologies if I've missed the obvious but the searches I did failed to retrieve anything that made sense to me.

I've been trying to use a parameter passed to a function, which makes an ajax call, as the name for the variable for where the data should go. For example:

ajaxFun("[url loctaion].json?callback=?","main","mainData");

The idea is that the retrieved JSON object is then accessible using the 'mainData' variable, however I can't use this explicitly as I have another file to retrieve which should be made accessible under the variable name 'expData'. Both of these are global variables and are already declared when the page loads.

Below is the current code which does work, but I would like to abstract away the 'if, else if, else' block in the success portion of the ajax call:

    function ajaxFun(urlS,jsonpCallbackS,dataSource){
    $.ajax({
            type: "GET",
            url: urlS,
            async: false,
            jsonpCallback: jsonpCallbackS,
            contentType: "application/json",
            dataType: "jsonp",
            cache: false,
            success: function(source){
            if(dataSource === "expData"){
                expData = source;
            }
            else if(dataSource === "mainData"){
                mainData = source;
            }
            else{
                console.log('data not set');
            }
            },
            error: function(classData){
                console.log('error');
            }
    });
}

I've tried various methods including using eval() and appending the result to an object:

var dataCon = {};

function ajaxFun(urlS,jsonpCallbackS,dataSource){
    $.ajax({
            type: "GET",
            url: urlS,
            jsonpCallback: jsonpCallbackS,
            contentType: "application/json",
            dataType: "jsonp",
            cache: false,
            success: function(source){
                var name = dataSource;
                alert(name);
                dataCon.name = source;
                },
                error: function(classData){
                    console.log('error');
                }
    });
}

Using:

'ajaxFun("[url loctaion].json?callback=?","main","mainData");'

I get the alert 'mainData' but in the console.log for dataCon I can see that the value of 'source' has been put to the key called 'name'.

I'm a JS noob, so apologies if I've missed the obvious but the searches I did failed to retrieve anything that made sense to me.

I've been trying to use a parameter passed to a function, which makes an ajax call, as the name for the variable for where the data should go. For example:

ajaxFun("[url loctaion].json?callback=?","main","mainData");

The idea is that the retrieved JSON object is then accessible using the 'mainData' variable, however I can't use this explicitly as I have another file to retrieve which should be made accessible under the variable name 'expData'. Both of these are global variables and are already declared when the page loads.

Below is the current code which does work, but I would like to abstract away the 'if, else if, else' block in the success portion of the ajax call:

    function ajaxFun(urlS,jsonpCallbackS,dataSource){
    $.ajax({
            type: "GET",
            url: urlS,
            async: false,
            jsonpCallback: jsonpCallbackS,
            contentType: "application/json",
            dataType: "jsonp",
            cache: false,
            success: function(source){
            if(dataSource === "expData"){
                expData = source;
            }
            else if(dataSource === "mainData"){
                mainData = source;
            }
            else{
                console.log('data not set');
            }
            },
            error: function(classData){
                console.log('error');
            }
    });
}

I've tried various methods including using eval() and appending the result to an object:

var dataCon = {};

function ajaxFun(urlS,jsonpCallbackS,dataSource){
    $.ajax({
            type: "GET",
            url: urlS,
            jsonpCallback: jsonpCallbackS,
            contentType: "application/json",
            dataType: "jsonp",
            cache: false,
            success: function(source){
                var name = dataSource;
                alert(name);
                dataCon.name = source;
                },
                error: function(classData){
                    console.log('error');
                }
    });
}

Using:

'ajaxFun("[url loctaion].json?callback=?","main","mainData");'

I get the alert 'mainData' but in the console.log for dataCon I can see that the value of 'source' has been put to the key called 'name'.

Share Improve this question asked Jul 8, 2013 at 13:55 joeschjoesch 3471 gold badge6 silver badges18 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

Try this:

window[dataSource]

That will get the value of the variable in the string you have. For example:

dataSource = "something"
something = 5
alert(window[dataSource]) // alerts 5

Use square-bracket notation:

dataCon[name]

or

dataCon[source]

See the MDN documentation on accessing object properties

var someObj = {};
var keyName = "test";
someObj[keyName]

Square brackets to reference a key inside an object.

Instead of

dataCon.name = source;

do

dataCon[name] = source;

The members of Javascript objects are stored by their keys. So these are the same:

dataCon.name
dataCon["name"]

The cool thing is you can replace the key by a variable:

dataCon.name = "hi!";
var key = "name";
dataCon[key] = "hi again!";

This very nicely explained on the Mozilla website.

本文标签: javascriptUse a parameter passed to a function as a variable nameStack Overflow