我正在尝试学习OOP JavaScript概念。以下是我班级的代码:
var Dialog = function(selector, properties) {
this.dialog = $(selector);
this.isVisible = false;
this.build(selector, settings);
};
Dialog.prototype.build = function(selector, settings) {
var dialogContent = this.dialog.html();
dialog.empty();
};
这是我初始化类的方法:
var dialog1 = new Dialog(".dialog", {
title: "Confirm",
modal: false,
buttons: [
["cancel", "cancelButton", "No"],
["accept", "acceptButton", "Yes"]
]
});
控制台报告dialog
未定义。如何访问dialog
变量?
查看我的整个文件dialog.js
here
P.S。我目前正在阅读MDN文档,但我没有意识到我的代码出错了。我还尝试将this.build()
替换为this.build.call(this, /*..*/)
,但没有结果。
答案 0 :(得分:2)
在您发布的full pastebin中,您实际上并未使用this.dialog
。您正在使用:
var dialogContent = Dialog.prototype.dialog.html();
当然,原型不具备dialog
属性。如果您将其切换为this.dialog
,它应该可以正常工作。
答案 1 :(得分:1)
在您的代码中,第21行:
dialog.empty()
你做不到。您总是需要在其前面加this
。
this.dialog.empty()