Как скопировать все функции объекта Javascript в другой, следуя этому шаблону? - PullRequest
0 голосов
/ 12 июня 2019

Мне нужно воссоздать все функции, начиная с getXXX, в объекте Alexa для объекта более высокого уровня (handlerInput), следуя этой схеме:

        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 плюс дополнительные параметры.

1 Ответ

1 голос
/ 12 июня 2019

Вы можете многое сделать с помощью циклов и замыканий:

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 вы также можете просто перечислить все существующие свойства.

...