大家一定习惯于使用vb.net和c#来创建asp.net页面,但是不知大家知不知道我们还可以使用jscript.net编写asp.net.
这可能是自微软1996年推出基于ie3.0的jscript1.0以来在功能上最大的飞跃。jscript 传统上被用作开发客户端脚本。在internet上它普遍存在,特别是在active sever page(asp)中 。
当脚本变得很大的时候,程序员需要编写更有效的代码;并且程序变得越来越复杂,程序员往往受到jscript的局限性。
如果你对jscript很熟悉的话,你将很快的在.net平台上使用jscript.net,应为jscript.net就像是jscript的升级版,而不是一门新的语言。
jscript.net最新的特性是jscript.net是真正的编译语言。这使它完全可以与vb.net和c#相媲美。从语法方面,jscript.net改善了变量类型的定义,不但支持明确的变量类型定义,还支持模糊的变量类型定义。模糊定义是一项激动人心的技术,它可以分析你脚本中使用的变量,并且推断出变量的类型。这就意味着你可以使用未定义的变量并以更快的速度执行。
请参照下面的例子:
function test()
{
for (var x = 0; x < 100; x++)
{
print(x);
}
}
当jscript.net编译这个程序的时候,它分析变量x的用途并确定变量x只用做数值型,结果变量被安全的定义为数值型。技术进步在于把变量定义为数值型要明显优于把变量定义为generic object 或 variant。
为了实现jscript.net的推断变量类型的功能,你需要遵守以下几条规则!
一. 永远声明你的局部变量。这看起来像是显而易见的,但是这是非常重要的,jscript.net只能推断你的局部变量,而不是全局变量。如果你没有声明它,直接使用,它就将成为全局变量,将不能被优化。
二. 仅使用一种数据类型,如果你声明一个数值型变量,却用来储存字符型的数据,jscript将把该变量定义为generic object 或 variant。
//无法推断类型 -- glob 是一个全局变量
var glob = 42;
function myfunc()
{
//无法推断类型-- s 没有定义因此它被当作全局变量
s = "hello";
// 可以推断类型
var i = 0;
//无法推断类型—q被指派成其他的类型
var q = new date();
q = 3.14159;
}
尽管类型推断是非常好的功能,但是它还是有一定的缺点的。它无法帮助我们捕获类型不匹配或其他的错误。为了解决这个问题,jscript.net提供一种方法明确定义变量的类型。通过例子,你将很容易的了解它的使用方法。
// 定义数值类型
var myint : int = 42;
// 定义一个函数,返回一个字符串
function getname() : string
{
// 程序行
}
// 定义一个带两个参数的函数返回一个逻辑类型
function checknumber(dval : double) : boolean
{
// function code
}
下面给出一个完成的函数,仔细的体会一下。
function getconditions(strcity : string) : string
{
var now : date = new date();
switch (strcity.touppercase())
{
case "london":
if (now.getmonth() <= 7 || now.getmonth() >= 9)
{
return "overcast";
}
else
{
return "partly overcast and humid";
}
break;
case "seattle":
if (now.getmonth() == 7 && now.getday() == 4)
{
return "torrential rain";
}
else
{
return "rain";
}
break;
case "la":
return "smoggy";
break;
case "phoenix":
return "damn hot";
break;
default:
return "partly cloudy with a chance of showers";
}
}
使用jscript.net,你也可以定义其他.net架构的类型,通过引用命名空间和派生类可以向jscript.net引入新的数据类型。这样在两者的数据类型中就有可能产生重叠。参见下表:
boolean .net framework boolean / jscript boolean
number .net framework double / jscript number
string .net framework string / jscript string
int .net framework int32
long .net framework int64
float .net framework single
double .net framework double
object .net framework object / jscript object
date jscript date object
array jscript array
function jscript function object
在jscript中定义类通过类声明, 包含方法和对象和var 声明。对于类的派生通过下面两个程序的对比,你讲清楚地明白。
jscript 5.5 code
// simple object with no methods
function car(make, color, year)
{
this.make = make;
this.color = color;
this.year = year;
}
function car.prototype.getdescription()
{
return this.year + " " + this.color + " " + this.make;
}
// create and use a new car object
var mycar = new car("accord", "maroon", 1984);
print(mycar.getdescription());
jscript.net code
// wrap the function inside a class statement.
class car
{
var make : string;
var color : string;
var year : int;
function car(make, color, year)
{
this.make = make;
this.color = color;
this.year = year;
}
function getdescription()
{
return this.year + " " + this.color + " " + this.make;
}
}
var mycar = new car("accord", "maroon", 1984);
print(mycar.getdescription());
jscript.net还支持定义private和protected property通过get和set进行读写。
如下例:
class person
{
private var m_sname : string;
private var m_iage : int;
function person(name : string, age : int)
{
this.m_sname = name;
this.m_iage = age;
}
// name 只读
function get name() : string
{
return this.m_sname;
}
// age 读写但是只能用set
function get age() : int
{
return this.m_sage;
}
function set age(newage : int)
{
if ((newage >= 0) && (newage <= 110))
this.m_iage = newage;
else
throw newage + " is not a realistic age!";
}
}
var fred : person = new person("fred", 25);
print(fred.name);
print(fred.age);
// 这将产生一个编译错误,name是只读的。
fred.name = "paul";
// 这个将正常执行
fred.age = 26;
// 这将得到一个 run-time 错误, 值太大了
fred.age = 200;
jscript.net可以用jscript 或任意net 框架语言(如 c #,vb7.0) 通过增加extends主题词在类声明以后来继承和扩展现有类。这能力允许jscript.net非常容易地利用 net 平台的丰厚资源。为了说明这些,给出一个程序。这个程序扩展了net 框架的servicebase 类。
// 导入需要的.net命名空间
import system;
import system.serviceprocess;
import system.diagnostics;
import system.timers;
class simpleservice extends servicebase
{
private var timer : timer;
function simpleservice()
{
canpauseandcontinue = true;
servicename = "jscript service";
timer = new timer();
timer.interval = 1000;
timer.addontimer(ontimer);
}
protected override function onstart(args : string[])
{
eventlog.writeentry("jscript service started");
timer.enabled = true;
}
protected override function onstop()
{
eventlog.writeentry("jscript service stopped");
timer.enabled = false;
}
protected override function onpause()
{
eventlog.writeentry("jscript service paused");
timer.enabled = false;
}
protected override function oncontinue()
{
eventlog.writeentry("jscript service continued");
timer.enabled = true;
}
function ontimer(source : object, e : eventargs)
{
eventlog.writeentry("hello world from jscript!");
}
}
servicebase.run(new simpleservice());
如何在asp+中使用jscript.net这才是我们关键的问题。我们将通过一个例子来说明这个问题。
访问sqlserver数据库
第一个aps+例子是使用jscript.net和.net的数据访问类来访问sqlserver数据库,
这里我还将使用大家熟悉的<% %>格式来编写,访问pubs中的authors表,我知道这很简单但是它可以体现一些新的特性。
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.sql" %>
<%@ language="jscript" %>
<link rel="stylesheet" type="text/css" href="style.css">
<%
// 设置数据库连接
var myconnection:sqlconnection = new sqlconnection("server=scripting;uid=sa;pwd=;database=pubs");
// 执行查询
var mycommand:sqldatasetcommand = new sqldatasetcommand("select * from authors", myconnection);
// 声明变量
var ds:dataset = new dataset();
var mytable:datatable
var mycolumns:columnscollection
var mycol:datacolumn
var myrows:rowscollection
var myrow:datarow
// 通过filldataset方法获取数据
mycommand.filldataset(ds, "authors");
mytable = ds.tables[0]
%>
<h1>
<%=ds.tables[0].tablename%>
</h1>
<br>
<table>
<thead>
<tr>
<%
//在表格的最上面输出字段名
mycolumns = mytable.columns
for (mycol in mycolumns)
{
%>
<th class="spec">
<%=mycol.columnname%>
</th>
<%
}
%>
</tr>
</thead>
<%
// 输出所有的纪录
myrows = mytable.rows
for (myrow in myrows)
{
%>
<tr>
<%
for(var i:int=0;i<mycolumns.count;i++)
{
%>
<td class="spec">
<%=myrow[i]%>
</td>
<%
}
%>
</tr>
<%
}
%>
</table>
例子2
<%@ webservice language="jscript" class="weather"%>
import system
import system.web.services
class weather {
webmethodattribute function getconditions(strcity : string) : string
{
var now = new date();
switch (strcity.touppercase())
{
case "london":
if (now.getmonth() <= 7||now.getmonth() >=9)
{
return "overcast"
}
if
{
return "partly overcast"
}
break;
case "seattle":
if (now.getmonth() == 7 && now.getday()==4)
{
return "torrential rain"
}
else
{
return "rain"
}
break;
case "la":
return "smoggy"
break;
case "phoenix":
return "damn hot"
break;
default:
return "partly cloudy with a chance of showers"
}
}
}