我定义了以下类:
class Link {
Action: string;
DialogType: string;
constructor ($link: JQuery) {
this.Action = $link.attr('data-action') || '';
this.DialogType = $link.attr('data-dialogType') || '';
}
}
我有声明类实例的函数。一旦声明了类,我就可以使用智能感知并进行完整的类型检查:
function adminDialog($link: JQuery) {
var link = new Link($link);
link.Modal.MaxHeight = 600;
doDialogAjax(link);
}
在下面的 doDialogAjax 函数中,我仍然有完整的类型检查:
function doDialogAjax(link: Link) {
$.ajax( link.Url, {
cache: false,
context: { link: link },
dataType: 'html'
}).done(onDialogDone).fail(onDialogFail);
}
此时,如果我尝试访问,我会丢失类型检查this.link
:
function onDialogDone(data: any, textStatus: string, jqXHR: JQueryXHR) {
// no type checking. I can type anything after this.link
var x = this.link.abcdefg;
// I assign to link
var link: Link = this.link;
// Now I get checking
var a = link.Modal.MaxHeight; // allowed
var a = link.abcdefg; // error
// However will the following change the property of the link passed
// into the function. I assume not but I am not 100% sure.
link.Modal.MaxHeight = 999;
}
所以我的问题是如何对link
使用上下文传递给函数 onDialogDone 的值进行类型检查?我是否正确地说,在我在该函数中创建一个新的变量链接之后,对链接所做的任何更改都不会对作为 this.link 传入的对象进行?