正如你们中的一些人可能知道的那样,JScript.NET 为常见的 JavaScript 语言添加了很多功能……它添加了一些class
语法、一些类型标识符等。
我使用 JavaScript 的主要原因是因为我喜欢它的动态性、嵌套函数调用、变量作用域等......所有这些功能在 .NET 版本中似乎都可以正常工作,但是编写这种代码会给我带来Type mismatch
错误:
public class Test extends Form {
var btn : Button;
function Test() {
var anotherBtn = new Button;
anotherBtn.Text = "some text";
// This is where the Type mismatch happens
anotherBtn.add_Click(function(sender:Object, e:System.EventArgs) {
Close();
});
this.Controls.Add(anotherBtn);
}
}
Application.Run(new Test);
好像我不能像那样传递匿名函数。如果我add_Click
在类的某个地方定义回调,它会很好用!
我怎样才能避免这种情况?我想以我一直习惯的方式用 JavaScript 编写代码……我能以某种方式强制jsc.exe
编译器使用旧版本的 JScript 吗?
或者也许 a 中的函数class
不是常规的 JS 函数?这是怎么回事?