使用 JScript 5.5 而不是 JScript.NET 编写应用程序

发布于 2024-09-18 21:38:35 字数 864 浏览 4 评论 0原文

你们中的一些人可能知道 JScript.NET 为通用 JavaScript 语言添加了相当多的功能...它添加了一些 class 语法、一些类型标识符等。

我使用 JavaScript 的主要原因是因为我喜欢它的动态性、嵌套函数调用、变量作用域等等...所有这些功能似乎在 .NET 版本中都能正常工作,但是编写此类代码会给我带来类型不匹配错误:

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 吗?

或者 class 中的函数可能不是常规的 JS 函数?这是怎么回事?

As some of you may know JScript.NET adds quite a few features to the common JavaScript language... it adds some class syntax, some type identifier etc etc..

The main reason I use JavaScript is because I love how dynamic it is, nested function calls, variables scope etc etc... All these features seem to work fine in the .NET version, however writing this sort of code gives me Type mismatch error:

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);

It seems like I can't pass anonymous functions like that. If I define the add_Click callback somewhere in the class, it works great!

How can I avoid this? I would like to code in JavaScript the way I'm always used to... can I somehow force the jsc.exe compiler to use an older version of JScript?

Or maybe functions inside a class are not regular JS functions? What's going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简单 2024-09-25 21:38:35

不幸的是,JScript 匿名函数与 .NET 委托不兼容。正如您所说,类的方法不是普通函数,因为 Microsoft 希望使 JScript 类与每个 .NET 类兼容。在 JScript.NET 中进行事件处理是一个问题,微软让它变得复杂并放弃了开发这种语言:(

所以不幸的是你必须遵守微软给出的事件处理方法。但是你可以为这些控件创建自定义类,这 使用其他控件比

使用 WindowsForms 控件更难,因为这些其他控件需要引用委托,正如 MSDN 声称的那样,JScript 不支持委托(我也无法使它们工作)。

有时,设置事件处理程序的唯一方法是创建自定义的扩展类,它会重载事件调用的方法,

希望您会发现这个答案很有用

。 JScript 5.5(或更高版本)是可能的,但它被称为“HTML应用程序”,它是在特殊环境中运行的网页,它为您提供本地沙箱的权限:您可以管理文件,读/写Windows。注册表,通过外部控制您甚至可以使用游戏控制器或使用套接字通过互联网进行通信。但它仍然是一个交互式 HTML 页面。

Unfortunately, JScript anonymous functions aren't compatible with .NET delegates. As you said, methods of class aren't normal functions, because Microsoft wanted to make JScript classes compatible to every .NET class. It is a problem to do event handling in JScript.NET, Microsoft made it complicated and dumped developing this language :(

So unfortunately you have to comply with methods of event handling given by Microsoft. But you could make custom classes for these controls, which could behave as you would want.

With other controls is even harder than with WindowsForms controls, because these other controls require reference to delegate, which, as MSDN claims, aren't supported by JScript (I also couldn't make them work).

Sometimes the only way to set event handler is to make custom, extending class, which overloads method called by the event.

I hope you will find this answer useful.

UPDATE:

You mentioned in title to write applications in JScript 5.5 (or higher version). It is possible but in limited way. It is called "HTML Application", it is webpage run in special environment which gives to you permissions of local sandbox: you can manage files, read/write to Windows Registry, with external controls you can even use game controllers or use sockets to communicate by the Internet. But it still will be an interactive HTML page.

水水月牙 2024-09-25 21:38:35

另一种可能性也许是使用 /fast- 进行编译(默认为 /fast+)。

jsc /? 的部分输出表示:

                         - CODE GENERATION -
/debug[+|-]              Emit debugging information
/fast[+|-]               Disable language features to allow better code generation
/warnaserror[+|-]        Treat warnings as errors
/w[arn]:<level>          Set warning level (0-4)

不过,这可能会影响其他一些不错的 .NET 功能。

The other possibility, perhaps, would be to compile with /fast- (the default is /fast+).

Part of the output from jsc /? says:

                         - CODE GENERATION -
/debug[+|-]              Emit debugging information
/fast[+|-]               Disable language features to allow better code generation
/warnaserror[+|-]        Treat warnings as errors
/w[arn]:<level>          Set warning level (0-4)

That may knobble some of the other nice .NET goodies, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文