我需要按照以下模式在更高级别的对象(handlerInput)上的Alexa对象中重新创建以getXXX开头的所有函数:
handlerInput.getLocale = function getLocale() {
return Alexa.getLocale(handlerInput.requestEnvelope);
}
handlerInput.getRequestType = function getRequestType() {
return Alexa.getRequestType(handlerInput.requestEnvelope);
}
handlerInput.getIntentName = function getIntentName() {
return Alexa.getIntentName(handlerInput.requestEnvelope);
}
handlerInput.getAccountLinkingAccessToken = function getAccountLinkingAccessToken() {
return Alexa.getAccountLinkingAccessToken(handlerInput.requestEnvelope);
}
handlerInput.getApiAccessToken = function getApiAccessToken() {
return Alexa.getApiAccessToken(handlerInput.requestEnvelope);
}
handlerInput.getDeviceId = function getDeviceId() {
return Alexa.getDeviceId(handlerInput.requestEnvelope);
}
handlerInput.getUserId = function getUserId() {
return Alexa.getUserId(handlerInput.requestEnvelope);
}
handlerInput.getDialogState = function getDialogState() {
return Alexa.getDialogState(handlerInput.requestEnvelope);
}
handlerInput.getSupportedInterfaces = function getSupportedInterfaces() {
return Alexa.getSupportedInterfaces(handlerInput.requestEnvelope);
}
handlerInput.isNewSession = function isNewSession() {
return Alexa.isNewSession(handlerInput.requestEnvelope);
}
handlerInput.getSlot = function getSlot(slotName) {
return Alexa.getSlot(handlerInput.requestEnvelope, slotName);
}
handlerInput.getSlotValue = function getSlotValue(slotName) {
return Alexa.getSlotValue(handlerInput.requestEnvelope, slotName);
}
handlerInput.escapeXmlCharacters = function escapeXmlCharacters(input) {
return Alexa.escapeXmlCharacters(input);
}
handlerInput.getViewportOrientation = function getViewportOrientation(width, height) {
return Alexa.getViewportOrientation(handlerInput.requestEnvelope, width, height);
}
handlerInput.getViewportSizeGroup = function getViewportSizeGroup(size) {
return Alexa.getViewportSizeGroup(size);
}
handlerInput.getViewportDpiGroup = function getViewportDpiGroup(dpi) {
return Alexa.getViewportDpiGroup(dpi);
}
handlerInput.getViewportProfile = function getViewportProfile() {
return Alexa.getViewportProfile(handlerInput.requestEnvelope);
}
因此,在我的代码中而不是使用
Alexa.getLocale(handlerInput.requestEnvelope)
,我只会执行handlerInput.getLocale()
。我无法修改这些功能,因为它们是SDK的一部分。我的运行时是节点8。上面的代码可以工作,但是我想知道是否有一种方法可以缩短代码并使用模式来批量执行此操作(也许每个函数都以get开头检测功能)。顺便说一句,大多数函数仅将handlerInput.requestEnvelope
作为参数,但有些仅采用字符串,有些则采用handlerInput.requestEnvelope
加上额外的参数。 最佳答案
您可以使用循环和闭包做很多事情:
for (const method of ["getLocale", "getRequestType", "getIntentName", "getAccountLinkingAccessToken", "getApiAccessToken", "getDeviceId", "getUserId", "getDialogState", "getSupportedInterfaces", "isNewSession", "getSlot", "getSlotValue", "getViewportOrientation", "getViewportProfile"]) {
handlerInput[method] = function(...args) {
return Alexa[method](handlerInput.requestEnvelope, ...args);
};
}
for (const method of ["escapeXmlCharacters", "getViewportSizeGroup", "getViewportDpiGroup"]) {
handlerInput[method] = Alexa[method].bind(Alexa);
}
根据您的要求和
Alexa
对象,您还可以简单地枚举所有现有属性。