天天看點

使您JavaScript成為Windows .exe

These days an average web developer needs to have a broad matrix of skills in order to do his/her job. HTML, CSS, JavaScript, AJAX, XML, some server side language, some linux skills, some apache, some database skills, standards, accessibility, SEO, the list goes on. Parts of the list are also multiplied by (or raised to the power of?) the number of browsers you want to support. Crazy, isn't it? We're such optimists trying to make stuff work in such an environment.

如今,一般的Web開發人員需要具備廣泛的技能才能完成工作。 HTML,CSS,JavaScript,AJAX,XML,一些伺服器端語言,一些linux技能,一些apache,一些資料庫技能,标準,可通路性,SEO,等等。 清單的一部分還乘以(或提高到?的幂)要支援的浏覽器數量。 瘋了,不是嗎? 我們是這樣的樂觀主義者,他們試圖在這種環境下工作。

There's gotta be an easier way to write code that does something meaningful! Yes, there is, it's called JavaScript. You learn JavaScript, you learn it well, and you don't need to learn anything else. Isn't that cool? JavaScript is, practically, everywhere. Learn JavaScript and you can:

必須有一種更簡單的方法來編寫可實作有意義的代碼! 是的,這就是所謂JavaScript。 您學習JavaScript,學習得很好,并且不需要學習其他任何東西。 那不是很酷嗎? 實際上,JavaScript無處不在。 學習JavaScript,您可以:

  • create rich and powerful web applications (the kind that runs inside the browser)

    建立豐富且功能強大的Web應用程式(在那種在浏覽器内運作)

  • write server-side code such as ASP scripts or for example code that is run using Rhino (A JavaScript engine written in Java)

    編寫伺服器端代碼(例如ASP腳本)或使用Rhino(用Java編寫JavaScript引擎)運作的代碼

  • create rich media applications (Flash, Flex) using ActionScript which is based on ECMAScript, which is JavaScript

    使用基于ECMAScript(JavaScript)的ActionScript建立富媒體應用程式(Flash,Flex)

  • write scripts that automate administrative tasks on your Windows desktop, using Windows Scripting Host

    使用Windows腳本宿主編寫在Windows桌面上自動執行管理任務的腳本

  • write extensions/plugins for a plethora of desktop application such as Firefox or Dreamweaver

    為大量的桌面應用程式(例如Firefox或Dreamweaver)編寫擴充程式/插件

  • create web applications that store information off-line on user's desktop, using Google Gears

    使用Google Gears建立可将資訊離線存儲在使用者桌面上的Web應用程式

  • create Yahoo!, or Mac, or dunno-what-type-of widgets

    建立Yahoo!,Mac或dunno-what-type-of小部件

  • create Windows apps (those that end in .exe) and libraries (.dll)

    建立Windows應用程式(以.exe結尾的應用程式)和庫(.dll)

I'm sure the list above is not even complete.

我确定上面的清單還不完整。

OK, it's a joke that with one programming skill only you'll be employed for life, but it's a fun thought anyway. Off to the main topic of the post.

好吧,開個玩笑說,隻有一種程式設計技能才能讓您終身受雇,但這還是很有趣的想法。 轉到文章的主要主題。

腳本 (JScript)

This is Microsoft's version of JavaScript (yep, the thing that annoys us *sometimes* in IE) and can also be used to create server side pages (ASP, ASP.NET) or desktop applications. Apparently JScript is now called JScript.NET and can be compiled to create .exe files. Let's see how.

這是MicrosoftJavaScript版本(是的,在IE中有時會惹惱我們),也可用于建立伺服器端頁面(ASP,ASP.NET)或桌面應用程式。 顯然,JScript現在稱為JScript.NET,可以進行編譯以建立.exe檔案。 讓我們看看如何。

編譯器 (The compiler)

The compiler (program that creates programs) is an exe file called jsc.exe (JScriptCompiler) and is part of the .NET framework. Good news is that you can use it without installing any MS IDE (whatever Visual Studio is called these days), free of charge. Even better, maybe it's already there, on your machine. I searched my completely normal Windows XP machine that doesn't have any special MS tools and was able to find two copies of the compiler! You can search for "jsc.exe" and in case you don't already have it, you can read how to get it 在此處閱讀如何擷取它。

So once you find your jsc.exe (found one o' mine in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727), then add this path to your environment path: Right-click My Computer - Advanced - Environment Variables - System Variables - Path - Edit

是以,一旦找到jsc.exe(在C:\ WINDOWS \ Microsoft.NET \ Framework \ v2.0.50727中找到一個),然後将此路徑添加到您的環境路徑中:右鍵單擊我的電腦-進階-環境變量-系統變量-路徑-編輯

Now open command prompt (Start - Run - "cmd" - OK) and type "jsc"+ENTER. You should see a list of help options for the compiler. Cool!

現在打開指令提示符(開始-運作-“ cmd”-确定),然後鍵入“ jsc” + ENTER。 您應該看到編譯器的幫助選項清單。 涼!

第一個.exe(以年為機關) (First .exe (in years))

The last time I created an .exe file is probably yeeears ago, when I was this Visual Basic king, writing a desktop application that takes a directory of images and creates a web gallery (example)

我上次建立.exe檔案的時間可能是在幾年前,那時我是Visual Basic的王者,他編寫了一個桌面應用程式,該應用程式使用一個圖像目錄并建立一個網絡畫廊(示例)

OK, lets create a simple application.

好的,讓我們建立一個簡單的應用程式。

cd ..
mkdir myapps
cd myapps
           

Create a file called hello.js with the following content:

建立一個具有以下内容的名為hello.js的檔案:

var d = new Date();
var n = Math.random();
print('Hello, \\ntoday is ' + d + '\\nand this is random - ' + n);
           

Now let's compile!

現在開始編譯!

C:\\myapps>jsc hello.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.
           

No error messages, so we'll take that as an OK 😉 Let's check:

沒有錯誤消息,是以我們将其作為OK😉讓我們檢查一下:

C:\\myapps>dir
 Volume in drive C has no label.
 Volume Serial Number is B96A-95DB

 Directory of C:\\myapps

08/31/2007  07:33 PM    <DIR>          .
08/31/2007  07:33 PM    <DIR>          ..
08/31/2007  07:34 PM             4,096 hello.exe
08/31/2007  07:33 PM               109 hello.js
               2 File(s)          4,205 bytes
               2 Dir(s)  40,287,092,736 bytes free
           

YES! An .exe was created! Without further ado, let's run it already!

是! 已建立一個.exe! 事不宜遲,讓我們開始運作吧!

C:\\myapps>hello
Hello,
today is Fri Aug 31 19:34:32 PDT 2007
and this is random - 0.5855108083158316
           

That's so cool, the compiled program works!

太酷了,編譯後的程式可以工作了!

制作DLL (Making a DLL)

Now, we're convinced that we have a good thing going here, so let's create a DLL, meaning create a library that other applications can use.

現在,我們确信這裡有一件好事,是以讓我們建立一個DLL,即建立一個其他應用程式可以使用的庫。

JScript.NET has the notion of namespaces and packages (which we usually fake on the web) and class-based objects (eww! well, it supports the prototype stuff as well). So if we simply wrap our code in a package and a class and we create a new file LibHello.js:

JScript.NET具有名稱空間和程式包(我們通常在網絡上僞造)和基于類的對象(ew !,它也支援原型的東西)的概念。 是以,如果我們僅将代碼包裝在一個包和一個類中,然後建立一個新檔案LibHello.js:

package LibHello {
    class Hello {
        function say() {
            var d = new Date();
            var n = Math.random();
            return 'Hello, \\ntoday is ' + d + '\\nand this is random - ' + n;
        }
    }
}
           

Let's compile this into a library, we need the /t:library option when compiling

讓我們将其編譯為一個庫,編譯時需要/ t:library選項

C:\\myapps>jsc /t:library LibHello.js
           

This creates hello.dll and we have a library!

這将建立hello.dll,我們有一個庫!

消耗lib (Consuming the lib)

Finally, let's create an app that leverages the new library we just created.

最後,讓我們建立一個利用我們剛剛建立的新庫的應用程式。

Create consumer.js with the following:

使用以下指令建立consumer.js:

import LibHello;
var h = new LibHello.Hello();
print(h.say());
           

Compile and run:

編譯并運作:

C:\\myapps>jsc consumer.js

C:\\myapps>consumer.exe
Hello,
today is Fri Aug 31 19:53:29 PDT 2007
and this is random - 0.45013379838789525
           

Nice and easy.

好,易于。

是以呢? (So what?)

I didn't have time to experiment, but I'm pretty sure you can take tools such as jsmin or jslint and easily compile them into libraries that can be consumed from windows apps, or VBA scripts in Access, Powerpoint, etc. Imagine you're writing some documentation in Word, you select some JS code you just wrote and JSlint it. That would be nice.

我沒有時間進行實驗,但是我很确定您可以使用jsmin或jslint之類的工具并将其輕松編譯為可從Windows應用程式或Access,Powerpoint等中的VBA腳本使用的庫。在Word中編寫一些文檔,選擇一些剛編寫的JS代碼并對其進行JSlint處理。 那樣就好了。

BTW, remember how we used /t:library option to produce a .dll and not an .exe? Well, there's also the option /t:winexe which creates a windows application I mean with the window and everything and not a console app. OK, let's give it a shot, create win.js with the following:

順便說一句,還記得我們如何使用/ t:library選項生成.dll而不是.exe嗎? 好吧,還有/ t:winexe選項,它建立一個我指的是帶有視窗和所有内容的Windows應用程式,而不是控制台應用程式。 好,讓我們試一下,使用以下指令建立win.js:

import System.Windows.Forms; // this has a MessageBox class
import LibHello;

var h = new LibHello.Hello();
MessageBox.Show(
        h.say(),
        "Dude!",
        MessageBoxButtons.OK,
        MessageBoxIcon.Exclamation
);
           

Compile:

編譯:

Double click in windows explorer and you have a nice little unquestionably useful Windows application 😉

輕按兩下Windows資料總管,您将獲得一個毫無疑問的好用的Windows應用程式😉

Tell your friends about this post on Facebook and Twitter

在Facebook和Twitter上告訴您的朋友有關此文章的資訊

翻譯自: https://www.phpied.com/make-your-javascript-a-windows-exe/