因此,我正在尝试访问类中函数中的两个函数。 modal_1() 如下代码所示。 console.log("inside 1st"); 工作正常,但是当我尝试进一步操作时(例如 modal_1.show_modal_1(); ),它会给我以下错误消息:
模态类:
const Modals = class {
modal_1(){
console.log("inside 1st");
var hide_modal_1 = function () {
console.log("hide modal 1");
//document.getElementById("modal_setup3DScene").style.display = "none";
};
var show_modal_1 = function() {
console.log("show modal 1");
//document.getElementById("modal_1-content").style.display = "block";
};
}
};
我用什么来调用 Modals 类:
let modals_List;
function setup() {
modals_List = new Modals();
modals_List.modal_1().show_modal_1();
}
最佳答案
您没有在函数 modal_1
中显式返回某些内容,因此返回值将为未定义
。然后您尝试在 undefined
上调用 show_modal_1()
。由于该属性不存在,因此您会收到此错误。
可以通过返回函数来解决,如下:
const Modals = class {
modal_1() {
console.log("inside 1st");
return {
hide_modal_1: functio () {
console.log("hide modal 1");
},
show_modal_1: function() {
console.log("show modal 1");
}
}
}
};
关于javascript - 如何在 JavaScript 中声明 ClassName.FunctionName.myFunction() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54449248/