您的位置:首页 > Web前端 > JavaScript

基于JavaScript的公式解释器 - 2 【完善自己的类型系统】

2010-10-18 22:06 405 查看
这里对万物之始Object进行改造,以形成自己的类型系统,方便后面的类型实现、识别、处理等等。



格式约定

先说明一下格式约定:

1. 全局常量定义:

以字符'$'开始,全部大写的格式。

var $MY_CONST = "ABCDEFG";




2. 全局函数定义:

以字符'$'开始,首字母大写的格式。

function $A(arg) {
    ...
};




3. 类的定义:

使用单词首字母大写的格式。



function MyClass {
};




4. 类内部方法及成员定义:

内部方法类似于private或protected,以一个'_'开始,第一单词小写,以后单词首字母大写的格式。

function MyClass() {
    ...
    this._myFunc = function(arg) {
        ...
    };
}; // MyClass




5. 类外部方法定义:

外部方法类似于public,使用单词首字母大写的格式。



MyClass.prototype.MyFunc = function(arg) {
    ...
}; // MyFunc




6. 属性访问方法定义:

属性访问方法使用get_/set_加属性名的格式,其中get_表示读方法,set_表示写方法。

...

get_MyProp: function() { return this._myProp; },
set_MyProp: function(val) { this._myProp = val; },

...


7. 文件内部方法,不应在该文件范围使用:

以两个'_'开始,首单词小写,以后每个单词首字母大写的格式。

function __myInternalFunc(arg) {
    ...
}; // __myInternalFunc










对Object的修改



Exception异常类:

文件:Exception.js

// Class: Exception
function Exception(obj, func, msg)
{
     return new Error($T(obj) + "." + func + ":" + msg);
};




Object类的改造:

文件:ObjectBase.js

// JScript source code

// Return class name of an object.
Object.prototype.get_ClassName = function() { 
    return this._className == null ? typeof(this) : this._className; 
};

// Set name of class, should not be called after the ctor.
Object.prototype.set_ClassName = function(name) { 
    this._className = name; 
};

// Copy members, properties and function from parent class.
Object.prototype.DeriveFrom= function(base) {
    if (arguments.length != 1 || base == null)
        throw new Exception(this, "DeriveFrom", "Derive from null");

    if (this._ancestors == null)
        this._ancestors = { "Object": new Object() };

    if (this._ancestors[base.get_ClassName()] != null)
        throw new Exception(this, "DeriveFrom", "Derive from derived class " + base.get_ClassName());

    // Copy ancestors.
    var ancesotrs = base.get_Ancestors();
    for (var v in ancesotrs)
        if (this._ancestors[v] == null)
        this._ancestors[v] = ancesotrs[v];

    // Derive
    if (this._ancestors[base.get_ClassName()] == null)
        this._ancestors[base.get_ClassName()] = base;

    // Copy other object
    for (var vName in base) {
        if (this[vName] == null)
            this[vName] = base[vName];
    } // for
}; // function DeriveFrom

// Get the ancestors of an object.
Object.prototype.get_Ancestors = function() {
    if (this._ancestors != null) {
        var res = new Array();

        for (var v in this._ancestors)
            res[v] = this._ancestors[v];
            
        return res;
    }
    else
        return [{ "Object" : new Object()}];
}; // function get_Ancestors

// check if a class has an ancestor.
Object.prototype.HasAncestor = function(baseClassName) {
    if (this._ancestors == null)
        return false;

    return this._ancestors[baseClassName] != null;
};  // function HasAncestor

// Return the internal _value object as string.
Object.prototype.ToString = function() {
    if (this._value != null)
        return this._value.toString();
    else
        return this.toString();
};

// Alphabet, used for $Ord, $L
var $ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

// Return the char code of letter.
function $Ord(input, index) {
    switch (arguments.length) {
        case 0: throw new Exception(this, "$Ord", "Argument(s) needed");
        case 1: return input.charCodeAt(0);
        case 2: return input.charCodeAt(index);
        default: throw new Exception(this, "$Ord", "Not implemented");
    }
};

// Return the char at pos
function $Chr(input, index) {
    switch (arguments.length) {
        case 0: throw new Exception(this, "$Chr", "Argument(s) needed");
        case 1: return input.charAt(0);
        case 2: return input.charAt(index);
        default: throw new Exception(this, "$Chr", "Not implemented");
    }
};

// Return if a char is in alphabet.
function $L(input, index) {
    switch (arguments.length) {
        case 0: throw new Exception(this, "$L", "Argument(s) needed");
        case 1:
            {
                var code = $Ord(input.toUpperCase());

                return code >= $Ord('A') && code <= $Ord('Z');
            }
            break;
        case 2:
            {
                var code = $Ord(input.toUpperCase(), index);
                return code >= $Ord('A') && code <= $Ord('Z');
            }
            break;
        default: throw new Exception(this, "$L", "Not implemented");
    }
}

// Return type of class, of course, it's a string.
function $T(input) {
    var res = input.get_ClassName();

    if (res == "object")
        return typeof (input);
    else
        return res;
}

// Assert
function $ASSERT(option) {
    if (!option)
        alert("Failed to assert:" + option.toString());
}




SystemDebug调试输出类:

文件:Debug.js

// JScript source code

// ------------- Global Object --------------
var $Debug = null;

// ------------- Debug ----------------
function SystemDebug(dbgCtrl) {
    this._ctrl = dbgCtrl;
};

SystemDebug.prototype =
{
    Write: function(str) {
        if (this._ctrl != null && str != null) {
            this._ctrl.value += str;
        }
    }, // function Write

    WriteLine: function(str) {
        if (this._ctrl != null) {
            if (str == null)
                this._ctrl.value += "/r/n";
            else
                this._ctrl.value += str + "/r/n";
        } // if
    }, // function WriteLine

    Select: function() {
        if (this._ctrl != null) {
            this._ctrl.select();
        }
    }, // function Select

    Enable: function(option) {
        if (this._ctrl != null) {
            this._ctrl.disabled = !option;
        }
    }, // function Enable

    Enabled: function() {
        return this._ctrl != null ? !this._ctrl.disabled : false;
    }, // function Enabled

    Clear: function() {
        if (this._ctrl != null)
            this._ctrl.value = "";
    } // function Clear
}; // prototype
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: