我正在尝试编写一个可以在几个不同对象中记录一些有用数据的通用函数,但我不想传递每个对象唯一的参数。理想情况下,对象的初始名称(如LaunchRequestHandler)将是一个非常充足的数据。如何访问此对象的名称?
const LaunchRequestHandler = {
canHandle(handlerInput) {
//I want to be able to know the name of the outer parent here.
//In this case, I would expect to retrieve 'LaunchRequestHandler'
logCanHandle(handlerInput);
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
}
答案 0 :(得分:0)
我真的不明白你想做什么,但也许你可以用ES6课程做点什么,如下:
class LaunchRequestHandler {
canHandle() {
console.log(this.constructor.name);
}
};
const x = new LaunchRequestHandler();
x.canHandle(); // outputs 'LaunchRequestHandler'