我想从另一个处理函数调用var

时间:2019-06-19 10:50:09

标签: javascript node.js function logic alexa

我正在做一个项目,需要从另一个函数调用变量,我不知道如何调用它,这是我想从HelloWorldIntentHandler调用team1,team2,server的代码。

const HelloWorldIntentHandler = {
canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
        && handlerInput.requestEnvelope.request.intent.name === 'HelloWorldIntent';
},
handle(handlerInput) {

    var team1 =  handlerInput.requestEnvelope.request.intent.slots.score.value;
    var team2 =  handlerInput.requestEnvelope.request.intent.slots.scoreone.value;
    var server = handlerInput.requestEnvelope.request.intent.slots.scoretwo.value;
    var value1 = Number(team1);
    var value2 = Number(team2);
    var totalteam1 = Math.round(value1 + value1 );
    var totalteam2 = Math.round(value2 + value2 );
    return handlerInput.responseBuilder
        .speak("score is " + totalteam1)
        .reprompt()
        .getResponse();
}

};

const PickleballScoreNowHandler = {
canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
        && handlerInput.requestEnvelope.request.intent.name === 'PickleballScoreNow';
},
handle(handlerInput) {

    var team1 =  handlerInput.requestEnvelope.request.intent.slots.score.value;
    var team2 =  handlerInput.requestEnvelope.request.intent.slots.scoreone.value;
    var server = handlerInput.requestEnvelope.request.intent.slots.scoretwo.value;
    return handlerInput.responseBuilder
        .speak("score is " + team1 + team2 + server)
        .reprompt()
        .getResponse();
}

};

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

const HelloWorldIntentHandler = {
    var score={};

   canHandle(handlerInput) {
          return handlerInput.requestEnvelope.request.type === 'IntentRequest'
         && handlerInput.requestEnvelope.request.intent.name === 'HelloWorldIntent';
        },

 handle(handlerInput) {

**score.team1 =  handlerInput.requestEnvelope.request.intent.slots.score.value;
score.team2 =  handlerInput.requestEnvelope.request.intent.slots.scoreone.value;
score.server = handlerInput.requestEnvelope.request.intent.slots.scoretwo.value;**
var value1 = Number(team1);
var value2 = Number(team2);
var totalteam1 = Math.round(value1 + value1 );
var totalteam2 = Math.round(value2 + value2 );
return handlerInput.responseBuilder
    .speak("score is " + totalteam1)
    .reprompt()
    .getResponse();
 }

-您可以这样访问所有变量。

  HelloWorldIntentHandler.score.team1