看msdn翻译的jscript的文档,能力有限翻译的一点都不好,自己发布出来!

 

Jscript8.0

创建你自己的类:

1Class-based Objects

类的字段

Class myClass

{

         const answer : int = 42;

         var distance : double;

}

Var c : myClass = new myClass;

c.distance = 5.3;

print(“The answer is ” + c.answer);

print(“The distance is ” + c.distance);

类的函数

Class myClass

{

         Const answer : int = 42;

         Var distance : double;

         Function sayHello(): string

{

         Return “Hello”;

}

}

Var c : myClass = new myClass;

c.distance = 5.2;

print(c.sayHello() + “, the answer is ” + c.answer);

类的构造函数

Class myClass

{

         Const answer :int = 42;

         Var distance : double;

         Function sayHello() : string

         {

                   Return “Hello”;

         }

         Function myClass(distance : double)

         {

                   This.distance = distance;

         }

}

Var c : myClass = new myClass;

Print (“The distance is ” + c.distance); encapsulation

Jscript object 的是用来封装数据和功能的。Objectpropertiesmethods组成。

Propertiesobject的数据成员。Methods的功能是用来操作数据或object.Jscript支持五类object:intrinsic objects,Prototype-base objects,class-based objects,host objects .NET Framework Classes.

New操作符关联调用被选择object的构造函数和初始化一个object实例。

Var myObject = new Object();

Var birthday = new Date(1961,5,10);

Var myCar : Car = new Car(“Pinto”);

Jscript 支持两类用户自定义objects(class-based and prototype-based).

两种类型都有各自的优缺点。Prototype-based objects是动态可扩展的,但是他们

2Prototype-based Objects

Constructors with Properties:

Jscript的强大特性是有能力自定义Prototype-based objects的构造函数。为了创建一个Prototype-based objects的实例,你首先必须一个构造函数。这个作用是创建一个新的object和初始化它(创建属性和赋值)。当被执行完,返回一个被构造的object的引用,在构造函数中,使用”this”关键字来引用被创建的对象。

Function pasta(grain,width,shape,hasEgg)

{

         this.grain = grain;

         this.width = width;

         this.shape = shape;

         this.hasEgg = hasEgg;

}

定义一个object构造函数之后,你就可以使用new 操作符创建这个object的实例。

Var spaghetti = new pasta(“wheat”,0.2,”circle”,true);

Var linguine = new pasta(“wheat”,0.3,”oval”, true);

你可以动态的添加属性到一个object实例,但是这些改变只能影响当前的这个实例。

Spaghetti,color = “pale straw”;

假如没有在改变改造函数,你想添加一个额外的属性到所有的对象实例,你可以添加属性到构造的Prototype object .

Pasta.prototype.foodgroup = “carbohydrates”;

Constructors with Methods:

可以在自定义的object中包含method.一个方法是在构造函数中包含一个属性,这个属性引用在别处定义的函数。像这样的构造函数,利用this关键字来引用当前的对象。

下面的例子扩展的pasta的构造函数中定义了一个toString函数,调用一个函数将显示object的值。

Function pasta(grain, width ,shape, hasEgg)

{

         This.grain = grain;

         This.width = width;

         This.shape = shape;

         This.hasEgg = hasEgg;

         This.toString = pastaToString;

}

Function pastaToString()

{

         Return “grain:”+this.grain ;

}

Var spaghetti = new pasta(“wheat”, 0.2, “circle”, true);

Print(spaghetti.toString());

Print(spaghetti);

3Jscript Modifiers

Jscript Modifiers 改变classes, interfaces 或者 classes, interfaces成员的行为和可见性。当定义classes interfaces使用 Modifiers ,但是Modifiers也不是必需的。

Visibility Modifiers 制约外边的代码访问classes, interfaces 和他们的成员。

Visibility modifiers 不能应用到全局函数或变量,只能使用Protected internal.

Visibility Modifier

Valid for

Meaning

public

class, class member, interface, interface member, or enumerations

成员是public的,任何代码都这个访问class.Jscript默认的classes,interfaces和他们的成员都是public

private

class member

成员在类内声明,对继承的类不可见。外边的代码不能访问private 成员

protected

class member

成员对内部成员可见,和继承类可见,protected modifiers 不能用于classes package scope.但是能应用到内嵌classes.

internal

class, class member, enumeration

在定义的package中的class,class member enumeration可见。

4Inheritance Modifiers

Inheritance Modifiers 控制继承classes methods properties override基类的methodsproperties.你可以管理继承classes methodsproperties将被override.

Inheritance Modifier

Valid for

Meaning

abstract

Class, method, or property

For methods or properties, this modifier indicates that the member does not have an implementation. For classes, this modifier indicates that there are one or more unimplemented methods. An abstract class or a class that contains an abstract member cannot be instantiated using the new keyword, but it can be used as a base class.

final

Class, method, or property

使用这个修饰的不能被 override.

JScript Data Types

Jscript支持13中原始数据类型和13中引用数据类型。另外你也可以声明新的数据类型,或者使用一些通用语言 Specification-compliant .NetFrameWork 数据类型。这部分包括关于固有的数据类型,怎么样扩展这些类型,怎么样定义属于你自己的数据类型,怎么输入数据,怎么样从一个数据类型转换到其他类型。

Array Data

Var arr = [1,2,3];

Var arr = [1,,,,,,5];

一个arrary literal 能包含任何数据类型,包含其他数组。

Var cats = [[“Names”,”Beansprout”,”Pumpkin”,”Max”],[“Ages”,6,5,4]];

Var arr = [1,,3];

Var arrI : int [] = [1,,3];

Var arrD : double [] = [1,,3];

Print(arr); //Displays 1,,3.

Print(arrI); //Display 1,0,3

Print(arrD); //Display 1, Nan,3

Integer array 中的空成员是0double array 中的空成员是Nan,其他类型的数组是Undefined.

Boolean Data

Boolean 数据类型只有两个值,分别是truefalse。一个boolean表示条件。

Using Boolean Values

Var s1 : string = “sam w.”;

Var s2 :string = “”;

While(true)

{

      If (s2.length < s1.length)

           S2= s2 + “*”;

Else

           Break;

}

Print(s1);

Print(s2);

Numeric Data

Jscript中有两类numeric data,分别是intergral data floatingpoint data.

Intergers 包括整数,负数,和0.他们可以用10进制数(decimal),8进制数(octal),16进制数(hexadecimal)来表示。

8进制值数是以o开头,16进制数是以ox开头

Number

Description

Decimal Equivalent

.0001, 0.0001, 1e-4, 1.0e-4

Four equivalent floating-point numbers.

0.0001

3.45e2

A floating-point number.

345

42

An integer.

42

0378

An integer. Although this looks like an octal number (it begins with a zero), 8 is not a valid octal digit, so the number is treated as a decimal. This produces a level 1 warning.

378

0377

An octal integer. Notice that although it appears to be only one less than the number above, its actual value is quite different.

255

0.0001, 00.0001

A floating point number. Even though this begins with a zero, it is not an octal number because it has a decimal point.

0.0001

0Xff

A hexadecimal integer.

255

0x37CF

A hexadecimal integer.

14287

0x3e7

A hexadecimal integer. Notice that the letter e is not treated as exponentiation.

999

0x3.45e2

This is an error. Hexadecimal numbers cannot have decimal parts.

N/A (compiler error)

Object Data

一个object literal 能初始化一个Jscript Object对象,表现形式由一个{}包围的中用逗号分隔的表达式。没有逗号分隔的成员由冒号类分隔值和属性,值可以是任何有效的Jscript表达式。

Using Object Data

下面的这个例子,就是初始化一个obj实例,并创建x,y连个属性成员,并分别为他们赋值。

Var obj = {x:1,y:2};

Object literals 可以被内嵌。例子如下

Var r= 3;

Var h =2;

Var cylinder = {height : h , radius : r,

sectionAreas : {top: 4 *math.PI* r* r,

bottom : 4* Math.PI*r*r,

side : 2 * Math.PI * r *R}};

object literal 不能用来初始化class-based object的实例。

Data Type Summary

Jscript提供了许多数据类型,这些数据类型能被分配到两个主要的类型,value 类型和reference类型。

Data Type Details

JScript value type

.NET Framework type

Storage size

Range

boolean

Boolean

N/A

true or false

char

Char

2 bytes

Any Unicode character

float (single-precision floating-point)

Single

4 bytes

Approximate range is -1038 to 1038 with accuracy of about 7 digits. Can represent numbers as small as 10-44.

Number, double (double-precision floating-point)

Double

8 bytes

Approximate range is -10308 to 10308 with accuracy of about 15 digits. Can represent numbers as small as 10-323.

decimal

Decimal

12 bytes (integral part)

Approximate range is -1028 to 1028 with accuracy of 28 digits. Can represent numbers as small as 10-28.

byte (unsigned)

Byte

1 byte

0 to 255

ushort (unsigned short integer)

UInt16

2 bytes

0 to 65,535

uint (unsigned integer)

UInt32

4 bytes

0 to 4,294,967,295

ulong (unsigned extended integer)

UInt64

8 bytes

0 to approximately 1020

sbyte (signed)

SByte

1 byte

-128 to 127

short (signed short integer)

Int16

2 bytes

-32,768 to 32,767

int (signed integer)

Int32

4 bytes

-2,147,483,648 to 2,147,483,647

long (signed extended integer)

Int64

8 bytes

Approximately -1019 to 1019

void

N/A

N/A

Used as the return type for a function that does not return a value

JScript reference type

.NET Framework type

Refers to

ActiveXObject

No direct equivalent

An Automation object.

Array

Interoperates with Array and typed arrays

Arrays of any type.

Boolean

Interoperates with Boolean

A Boolean value, either true or false.

Date

Interoperates with DateTime

Dates are implemented using the JScript Date object. The range is approximately 285,616 years on either side of January 1, 1970.

Enumerator

No direct equivalent

An enumeration of items in a collection. For backward compatibility only.

Error

No direct equivalent

An Error object.

Function

No direct equivalent

A Function object.

Number

Interoperates with Double

A numeric value with an approximate range of -10308 to 10308 and with an accuracy of about 15 digits. Can represent numbers as small as 10-323.

Object

Interoperates with Object

An Object reference.

RegExp

Interoperates with Regex

A regular expression object.

String Data Type (variable-length)

String

0 to approximately 2 billion Unicode characters. Each character is 16 bits (two bytes).

String Object (variable-length)

Interoperates with String

0 to approximately 2 billion Unicode characters. Each character is 16 bits (two bytes).

VBArray

No direct equivalent

A read-only Visual Basic array. For backward compatibility only.

User-Defined Data Types

有时你需要的数据类型Jscript没有提供,在这种情况下,你可以创建使用class 关键字创建一个新的class 或创建属于你自己的数据类型。

Defining a Data Type

Class myIntVector

{

      Var x : int;

      Var y : int;

      Function myIntVector(xIn : int, yIn : int)

      {

           X = xIn;

           Y = yIn;

      }

}

Function magnitude(xy: myIntVectore) :double

{

      Return (Math.sqrt(xy.x * xy.x + Xy.y * xy.y));

}

Var point : myIntVector = new myIntVector(3,4);

Print(magnitude(point));

Typed Arrays

一个typed array 是一个数据类型。每个typed array 必须有一个base 数据类型,数组中的每个成员必须是这个base 数据类型,base 数据类型也可以是数组。

Using Typed Arrays

Var names :string [] ;

Var things : object[] = new object[50];

Things[42] = new date();

Var matrix : int[][];

Matrix = new (int[])[5];

For(var I = 0 ; I < 5; I ++)

      Matrix[i]= new int[5];

matrix[2][3] = 6;

matrix[2][4] = 7;

var multidim : double [,,] = new double [5,4,3];

multidim [1,3,0] = math.PI*5;

package Statement

package deuschland

{

      Class Greeting

      {

           Static var hello : string = “Guten tag”;

}

}

import Statement

importnamespace

Asp.net

<%@ Import namespace = "mydll" %>

<%@ Assembly name = "mydll" %>

Declaring Typed Variables and Constants

Var count : int;

Var count :int = 1;

Const dayInWeek : int = 7;

Var count : int = 1, amount : int = 12; level:double = 4234.3;

Declaring Untyped Variables and Constants

Var count;

Scope of Variables and Constants

eval Method

var doTest : boolean = true;

var dateFn : String;

if(doTest)

   dateFn = "Date(1971,3,8)";

else

   dateFn = "Date()";

var mydate : Date;

eval("mydate = new "+dateFn+";");

print(mydate);

Thu Apr 8 00:00:00 PDT 1971

posted @ 2008-01-15 15:51  斯伯内德  阅读(491)  评论(0编辑  收藏  举报