最近在对JScript进行面向对象的扩展,使得大家以后可以在JScript(TM)中定义类,并写出真正的面向对象的代码。

我对扩展了的JScript取了名字,叫OxScript,打算实现的features如下:

OxScript is an Object-Oriented extension for JavaScript.
The following features will be provided by OxScript:
1. class definition;
2. final class;
3. abstract class;
4. inheritance;
5. overriding;
6. abstract function;
7. polymorphism;
8. package;
9. property getter/setter;
10. reflection(readonly);

大体的Specification如下:

[Access Modifiers]
public, protected, private,
static, abstract, final, virtual

[Directives]
$include, $import,
$package, $class,
$field, $method, $property, $event

[Additional Keywords]
extend, implement,
public, protected, private, static,
get, set

[Additional Operators]
classof, raise

[Predefined Classes]
class Package extend Object {
 public :
 protected :
 private :
}
class Class extend Object {
 public :
 protected :
 private :
}
class Member extend Object {
 public :
 protected :
 private :
}
class Field extend Member {
 public :
 protected :
 private :
}
class Method extend Member {
 public :
 protected :
 private :
}
class Property extend Member {
 public :
 protected :
 private :
}
class Event extend Member {
 public :
 protected :
 private :
}

[Class Hierarchy]
OxScript.Object 
     |--OxScript.Number 
     |--OxScript.String 
     |--OxScript.Boolean 
     |--OxScript.Date 
     |--OxScript.Array 
     |--OxScript.Enumerator 
     |--OxScript.Function 
     |--OxScript.RegExp 
     |--OxScript.VBArray 
     |--OxScript.Error 
     |--OxScript.ActiveXObject 
     | 
     |--OxScript.Reflection.Package 
     |--OxScript.Class 
     |--OxScript.Reflection.Member 
          |--OxScript.Reflection.Field 
          |--OxScript.Reflection.Method 
          |--OxScript.Reflection.Property 
          |--OxScript.Reflection.Event

下面是一段代码示例:


// namespace declarations
var
LaserPackage 
= $package();

with(LaserPackage) {
    
// define a class named OxScriptDemo in LaserPackage
    $class ([public, final], OxScriptDemo, {extend : OxScript.Object},
        
// construcltor
        function OxScriptDemo() {
            
// this is the constructor implementation of class LaserPackage.OxScriptDemo
        },
        {
            
// public members
            public : {
                
// a field with type of OxScript.Number
                field1 : $field (OxScript.Number),
                
// a method definition
                method1 : $method (
                    
function() {
                        
// implementation of method1
                    }
                ),
                
// a property of type OxScript.String having both getter and setter
                property1 : $property (
                    OxScript.String,
                    {
                        get : 
function() {
                            
// getter of property1
                        },
                        set : 
function() {
                            
// setter of property1
                        }
                    }
                ),
                
// an event declaration
                event1 : $event (
                    
function(source, eventArgs) {} // the function declaration of event1 handler
                ),
                
// an event handler for event1
                event1Handler : $method(
                    
function(source, eventArgs) {
                        
// implementation of event handler
                    }
                ),
                
// the method which demonstrats how to access the class members and raise events
                demoMethod : $method (
                    
function() {
                        
// assign a value to field1
                        this.field1 = 66;
                        
// call method1
                        this.method1();
                        
// get property1
                        var str = this.get_property1();
                        
// set property1
                        this.set_property1(str);
                        
// add a handler to event1
                        this.event1.add(this.event1Handler);
                        
// raise event1
                        raise (this.event1);
                    }
                )
            },
            
// protected members
            protected : {
                
// 
            },
            
// private members
            private : {
                
// 
            }
        }
    );
}


OxScript其实是巧妙了利用了JScript的灵活的语法和特性,对其进行了一层包装而已,所以不需要任何安装,只需要在页面中引用OxScript.js就可以享用模拟的面向对象编程了。另外OxScript还提供一个比较有用的特性就是可以反射出所类的定义信息,然后可以自动产生出类似VS中ObjectBrowser的参考窗体,罗列出当前页面中所用到的所有的类和它们的成员的定义信息,从而提供programming references来辅助脚本的编写。小弟目前正在全力的开发中(估计一个星期内就可以发布第一个版本了),先贴出设计思想和代码预览,供大家讨论和交流。请大家多提宝贵意见。
posted on 2005-03-25 22:53  Laser.NET  阅读(3913)  评论(10编辑  收藏  举报
无觅相关文章插件,快速提升流量