DWJB
DWJB

Reputation: 71

Assign javascript object property a value via string value on another object

I'm trying to assign myShuttle.command to a string called signIn which is being stored in the shuttleCommands function.

function buildAndLaunchSignInShuttle (credentialsDoc) {
    var myShuttle = new shuttle();
    myShuttle.command = shuttleCommands.signIn;
    myShuttle.document = credentialsDoc;
    var responseObject = server.launchShuttle(myShuttle);

    return responseObject;
}

function shuttleCommands() {
    return {
        signIn: "signIn",
        signOut: "signOut",
        getEntireCollection: "getEntireCollection",
        getSingleDocument: "getSingleDocument",
        saveDocument: "saveDocument"
    };
}

How do I go about doing this? I don't really know what the right way of phrasing this is which is hindering my Googling!

Upvotes: 0

Views: 34

Answers (1)

Michael Lorton
Michael Lorton

Reputation: 44376

myShuttle.command = shuttleCommands.signIn;

This will assign myShuttle.command to the signIn attribute of the shuttleCommands function itself, which is probably undefined.

I think you want to assign myShuttle.command to the signIn attribute of the value returned by the shuttleCommands function, which would be

myShuttle.command = shuttleCommands().signIn;

Upvotes: 1

Related Questions