【发布时间】:2016-04-25 02:35:38
【问题描述】:
我有一个类,我使用它的方法作为事件侦听器,所以我在方法中不使用this,而是使用that。
这是基类:
function Modal(){
var that = this;
this.opened = false;
this.open = function(){
that.opened = true;
var id = this.id;
// more code
};
this.close = function () {
if (that.opened) // do something;
};
}
这是继承的类:
function ModalBook(){
var that = this;
this.open = function(){
that.opened = true;
var id = this.id;
// more code
};
}
ModalBook.prototype = new Modal();
var modalBook = new ModalBook();
现在modal.opened 的值在模式打开时为真,但在关闭时为假。
在this.close这一行调试,我看到that是Modal的一个实例,modalBook是ModalBook的一个实例。
所以我的问题是:如何在modalBook 的两种方法中保留this 的值?
【问题讨论】:
-
你能为此制作一个小提琴吗?
标签: javascript class inheritance this