【发布时间】:2013-03-03 01:52:48
【问题描述】:
如果我在一个类中声明这个
class AlertConfigViewModel {
DeleteAlert = function (item) {
if (item) {
if (confirm('Are you sure you wish to delete this item?')) {
this.Alerts.remove(item);
}
}
else {
alert("something is wrong");
}
};
}
结果是这样的:
var AlertConfigViewModel = (function () {
function AlertConfigViewModel(json) {
this.DeleteAlert = function (item) {
if(item) {
if(confirm('Are you sure you wish to delete this item?')) {
this.Alerts.remove(item);
}
} else {
alert("something is wrong");
}
};
}
}
如果我在 AlertConfigViewModel 的上下文之外调用 AlertConfigViewModel 那么“this”不是我认为的 AlertConfigViewModel,因为它的内部函数 AlertConfigViewModel(
【问题讨论】:
-
函数中
this的值取决于它的调用方式。 -
请说明您对
this的使用存在问题。 -
你最后好像少了
)();
标签: javascript knockout.js typescript