简体   繁体   中英

How to set specific variable name?

If there is possibility to set a specific variable name in repeated function (object programming)?

I need to reuse this function and for every use I need the variable name to be specific (function property + a).

function test(test)
{
    var a + test = 'test text';
    console.log(atest);
}

test('test');

I need the variable atest to result in 'test text' .

What you want is a scoped namespace like myVariables to be used as a dictionary, eg

 // this prevents keys like `hasOwnProperty` from being pre-defined on namespace var myVariables = Object.create(null); function test(name) { myVariables['a' + name] = 'test text'; } test('test'); console.log(myVariables.atest);

this is really a bad programming but the answer is :

 window['a'+'test'] = 'test text'; console.log(atest);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM