Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create javascript object with custom name?

Tags:

javascript

It's a little bit difficult to explain what I need, so I'll use some non-working code:

function createSimpleObjet(name, value){
    return {
        name: value
    };
}

//create it
var obj = createSimpleObject('Message', 'Hello World!');
//test it:
alert(ojb.Message); //should alert 'Hello World!'

How would I go about?

like image 685
Kees C. Bakker Avatar asked Jul 16 '26 15:07

Kees C. Bakker


1 Answers

In order to do this try square bracket notation:

function createSimpleObject(name, value){
    var obj = {};
    obj[name] = value;
    return obj;
}
like image 169
VisioN Avatar answered Jul 18 '26 06:07

VisioN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!