7

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?

3

2 Answers 2

13

In order to do this try square bracket notation:

function createSimpleObject(name, value){
    var obj = {};
    obj[name] = value;
    return obj;
}
Sign up to request clarification or add additional context in comments.

Comments

4

You can't use a variable as a property name in an object literal. You have to create the object, and then assign the value using square bracket notation.

var object = {};
object[name] = value;

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.