Use JScript.net to write. net Applications

Source: Internet
Author: User
Tags ultraedit

Javascript is a magic language, flexible, easy to learn, and advanced in concept. But now it seems to have been pinned on the browser. In fact, this is the biggest misunderstanding of javascript. M $. the net platform provides full-featured JScript support. However, this JScript complies with the ECMA standard. Like M $'s attitude towards other international standards, JScript adds many other capabilities, so people accidentally bound them to windows. But it doesn't matter. Our topic this time is to write a. net program. It doesn't matter if it is bound. Who makes me a "drop-down to M $"? In fact, I basically don't want to go with money (we help people call it "Shangguan jinhong" ----- are you familiar with it ?)

After. net sdkis installed, jsc.exe is installed. Before official use, we need to set up an integrated compilation environment. bigtall uses UltraEdit, so a new command is created in the menu "Advanced/tool configuration: the menu item name is "JScript.net compilation"; Command Behavior C: \ Windows \ Microsoft. NET \ Framework \ v2.0.50727 \ jsc.exe/fast-"% f"; working directory: % p; remember to set "output" to "output to list box" and save it.

With the environment, we can start to write some test code. First, create a test. js, standard javascript code:

Var arr = ["a", "B"];
Var obj = {name: "test", value: 3.0 };

Print (arr [0]);
Print (arr [1]);
Print (obj. name );
Print (obj. value );

Run the following menu command to compile successfully. A test.exe file is displayed in the current directory. It runs normally!

If you want to compile a complete program, I am afraid one file is not enough. So let's test the compilation of multiple files and create a mod. js file with the following content:

Function hello ()
{
This. name = "haha ";
}

Function haha ()
{
Print ("call haha ");
}

Then we modify test. js as follows:

Var arr = ["a", "B"];
Var obj = {name: "test", value: 3.0 };

Print (arr [0]);
Print (arr [1]);
Print (obj. name );
Print (obj. value );

Import mod;

Var t = new hello ();
Print (t. name );

However, compilation is learned. We need to compile it manually. In cmd, enter jsc/fast-mod. js test.jsto compile a mod.exe file and run it! However, if we compile and run the jsc/fast-test. js mod.js command, the test.exe cannot be run because we cannot find things in mod. js. This is an important part of attention.

Because JScript has made a lot of extensions, we need to test whether the extension commands and non-extensions can cooperate with each other, because we need to compile. and. net programs.. net SDK. It is necessary to use extensions. Create a new file pkg. js with the following content:

Import System;
Package France. Paris {
Public class Landmark {
Static var Tower: String = "Eiffel Tower ";
Function p ()
{
System. Console. WriteLine ({t: "hello from writeline"}. t );
}
}
};

Modify test. js as follows:

Var arr = ["a", "B"];
Var obj = {name: "test", value: 3.0 };

Print (arr [0]);
Print (arr [1]);
Print (obj. name );
Print (obj. value );

Var t = new hello ();
Print (t. name );

Haha ();

Print (France. Paris. Landmark. Tower );

Import France. Paris;
New Landmark (). p ();

Run the command line jsc/fast-mod. js pkg. js test.jsto compile mod.exe. The running result is as follows:

D: \ work \ testjs.net> jsc/fast-mod. js pkg. js test. js
Microsoft (R) JScript Compiler version 8.00.50727
For Microsoft (R). NET Framework version 2.0.50727
Copyright (C) Microsoft Corporation 1996-2005. All rights reserved.

D: \ work \ testjs.net> mod
A
B
Test
3
Haha
Call haha
Eiffel Tower
Hello from writeline

D: \ work \ testjs.net>

Next, we need to do a job. In actual javascript programming, we have a few inconveniences. One is editing, and the downstream of eclipse is JsEclipse,. net is not available. Fortunately, vs2008 has come out, and the problem is not big. The second is debugging, which is surprisingly difficult. firefox has plug-ins, which is very good. Internet Explorer is also available, but it is not very easy to use. It often cannot grasp the breakpoint, but it has become bigger since vs2005. The third problem is the js language itself. Fortunately, there is a ready-made extension library prototype, and several other libraries, such as dojo, ext, and jquery, have also been used, only prototype is a pure extension for the js language itself, and other components are too closely bound to the browser and cannot be used. So we will compile prototype 1.5 as our extension library.

First, obtain the prototype1.5 code from here, use ultraedit to load it, and then run the "JScript.net compilation" command set at the beginning, resulting in a bunch of errors. It doesn't matter. We can do the following two jobs:

  1. Replace full-text, full-word, and case-sensitive searches with "set", "get" with "_ get", and "event" with "ev ".
  2. Generate the compitable. js file with the following content:

    Function fn (func): Function {return func ;}

    Var document = {
    GetElementById: function () {return null ;},
    CreateElement: function () {return {appendChild: function (){}};},
    CreateTextNode: function () {return {};},
    GetElementsByTagName: function () {return [];},
    AddEventListener: function (){},
    Write: function (){},
    All: [],
    Body :{},
    DocumentElement :{}
    };
    Var window = {
    ScrollTo: function (){},
    SetTimeout: function (){},
    AttachEvent: function (){},
    ClearInterval: function (){},
    SetInterval: function (){},
    Location: {href :""},
    PageXOffset: 0,
    PageYOffset: 0
    };

    Var navigator = {
    UserAgent :"",
    AppVersion :""
    };

  3. Use the command line jsc/debug/fast-mod. js pkg. js compitable. js prototype.1.5.js test. js compilation, there will be a bunch of warnings and six errors, are similar to function () {this. respondToReadyState (1 )}. bind (this) error, change them to fn (function () {this. respondToReadyState (1 )}). bind (this ). The possible cause may be a bug in the JScript compiler. In this environment, the function is actually a Function type.
    If you are using the latest prototype1.6, replace this with Case sensitivity in addition to the preceding steps. if the Element is Element, set the 1555 line var element = this. modify Element to var element = typeof Element = "undefined "? {}: Element; replace row 3845 wrapper. handler = handler; with fn (wrapper). handler = handler.

Next, modify the test. js file as follows:

Var arr = ["a", "B"];
Var obj = {name: "test", value: 3.0 };

Print (arr [0]);
Print (arr [1]);
Print (obj. name );
Print (obj. value );

Var t = new hello ();
Print (t. name );

Haha ();

Print (France. Paris. Landmark. Tower );

Import France. Paris;
New Landmark (). p ();

Var instance = {
Funca: function () {return "funca ";},
Funcb: function () {return "funcb ";}
};
Object. extend (instance ,{
Funcb: function () {return "override funcb ";},
FuncExt: function () {return "funcExt ";}
});
Print (instance. funca ());
Print (instance. funcb ());
Print (instance. funcExt ());

Recompile and run.

I would like to share my little experience with bigtall: if the program runs abnormally, it is generally caused by a null value, and the JScript exception report is not clear.

In addition, the dom-related parts of prototype cannot be used in ajax code, and setTimeout, alert, and other functions are not available unless you expand my compitable. js.

For the basic JScript.net help, refer to here. The same content is also available in the msdn help of VS2005.

In fact, to use JScript.net for desktop applications. net, but bigtall personally thinks that if we follow the winform interface specifications, JScript will lose its advantage. As js is so flexible, there should be a brand new interface library. You can refer to the Ruby GUI and Python GUI library practices. If you are interested, read this article.

Conclusion

By using the parameter/fast-and replacing a few reserved words, JScript can compile most traditional js Code and use it with the extension Syntax of JScript.net. This gives us the foundation to use JScript to compile the actual application.

Combined with the Language extension library in the current js field (prototype is used only currently), it can provide great convenience for JScript.net programming. However, currently, JScript.net lacks a GUI library and an XML library that is truly suitable for js features.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.