如何按照此模式将 Javascript 对象的所有功能复制到另一个对象?

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

我需要按照以下模式在更高级别的对象 (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 对象,您也可以简单地枚举所有现有属性。