gpt4 book ai didi

javascript - 如何按照这种模式将 Javascript 对象的所有函数复制到另一个对象?

转载 作者:行者123 更新时间:2023-12-02 23:29:48 25 4
gpt4 key购买 nike

我需要按照以下模式在更高级别的对象 (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。上面的代码可以工作,但我想知道是否有一种方法可以缩短代码并使用该模式批量执行此操作(也许使用 for every 函数检测以 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 对象,您也可以简单地枚举所有现有属性。

关于javascript - 如何按照这种模式将 Javascript 对象的所有函数复制到另一个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56563614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com