为什么不能使用函数输入作为变量来调用函数? [重复]

如何获得此函数以将num的值用作对短语的调用。 ?我将1指定为输入值,所以为什么不将其读为phrase.1 [0]?

var phrase = {
    1: "Breaking News! The world in crisis!"
    };
function writeText(num){
    var dialogue= phrase.num[0];
    dialogue= dialogue+ phrase.num[1];
    document.getElementById("dbox").innerHTML = dialogue; 

};
writeText(1);

甚至这个?

var phrase = {
    first: "Breaking News! The world in crisis!"
    };
function writeText(whichPhrase){
    document.getElementById("dbox").innerHTML = phrase.whichPhrase; 
};
writeText(first);
评论
  • 嘘!安静
    嘘!安静 回复

    var dialogue= phrase[num]; is what you need when you want your JS object key to be passed as parameter

    var phrase = {
        1: "Breaking News! The world in crisis!"
        };
    function writeText(num){
        var dialogue= phrase[num];
        document.getElementById("dbox").innerHTML = dialogue; 
    
    };
    writeText(1);
    <div id="dbox"></div>