通过另一个对象上的字符串值为javascript对象属性分配值

时间:2015-03-06 20:28:32

标签: javascript

我试图将myShuttle.command分配给名为signIn的字符串,该字符串存储在shuttleCommands函数中。

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"
    };
}

我该怎么做呢?我真的不知道正确的措辞是什么阻碍了我的谷歌搜索!

1 个答案:

答案 0 :(得分:1)

myShuttle.command = shuttleCommands.signIn;

这会将myShuttle.command分配给shuttleCommands函数本身的signIn属性,该属性可能未定义。

我想你想将myShuttle.command分配给的signIn属性 shuttleCommands函数返回的值,这将是

myShuttle.command = shuttleCommands().signIn;