当前位置:首页>网络学院>程序开发>c#教程>文章内容

用JScript.net写.net应用程序

[ 来源:http://www.it55.com | 作者: | 时间:2007-12-21 | 收藏 | 推荐 ] 【

 

 

  使用命令行jsc /fast- mod.js pkg.js test.js编译出mod.exe,运行结果如下:

  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。保留所有权利。
  D:\work\testjs.net>mod
  a
  b
  test
  3
  haha
  call haha
  Eiffel Tower
  hello from writeline
  D:\work\testjs.net>

  接下来,我们还要做一个工作。因为从实际的javascript编程中,我们有几个不方便的地方,一个是编辑,eclipse下游JsEclipse,但是.net下没有,好在vs2008出来了,问题不大了;第二就是调试,出奇的困难,firefox下有插件,很好。ie下也有,但是不太好用,经常抓不住断点,但是从vs2005开始也凑胡了,只是大了一点而已。第三个就是js语言本身的问题了,好在有现成的扩展库prototype,其他的几个库dojo,ext,jquery也都用过,只有prototype是纯面向js语言本身的扩展,其他几个跟浏览器绑定太紧密,用不了。所以我们接下来就要编译prototype 1.5作为我们的扩展库了。

  首先从这里获取prototype1.5的代码,用ultraedit装入,然后运行开头设置的“JScript.net编译”命令,出现一堆错误。不要紧,我们做如下的两个工作即可:

  全文、全词、大小写敏感查找替换set为_set,get为_get, event为ev

  生成compitable.js文件,内容如下:

 

 

  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:""
  };

  使用命令行jsc /debug /fast- mod.js pkg.js compitable.js prototype.1.5.js test.js编译,会有一堆警告和6个错误,都是在prototype中的类似function() { this.respondToReadyState(1) }.bind(this)的错误,把他们修改成fn(function() { this.respondToReadyState(1) }).bind(this)。估计原因可能是JScript编译器的一个bug,没能在这个环境下识别出function其实就是Function类型。

  如果使用的是最新的prototype1.6,除了上述几步外,还要大小写敏感替换this.Element为Element,把1555行var element = this.Element修改为var element = typeof Element == "undefined" ? {} : Element;把3845行wrapper.handler = handler;替换为fn(wrapper).handler = handler;即可。

  接下来我们修改test.js文件,如下:

 

 

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

(编辑:IT资讯之家 www.it55.com

返回顶部
共3页: 上一页 [1] 2 [3] 下一页  

网友评论

[以下评论为网友观点,不代表本站。请自觉遵守互联网相关政策法规,所有连带责任均有评论者自负。]
[不超过250字]