Search code examples
javascriptecmascript-6prototype

How to copy all functions of a Javascript object to a different one following this pattern?


I need to recreate all functions starting with getXXX in the Alexa object on a higher level object (handlerInput) following this pattern:

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

So in my code instead of using Alexa.getLocale(handlerInput.requestEnvelope) I would just do handlerInput.getLocale(). I can't modify these functions as they are part of an SDK. My runtime is node 8. The code above works but I was wondering if there's a way to shorten the code and use the pattern to do this in bulk (maybe with a for each function detecting the functions starting with get). BTW note that most functions just take handlerInput.requestEnvelope as parameter but some take a string only and some take handlerInput.requestEnvelope plus extra parameters.


Solution

  • You can do a lot with loops and closures:

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

    Depending on your requirements and the Alexa object, you might also simply enumerate all existing properties.