基於JavaScript的公式解譯器 – 2 【完善自己的類型系統】

來源:互聯網
上載者:User

這裡對萬物之始Object進行改造,以形成自己的類型系統,方便後面的類型實現、識別、處理等等。

 

格式約定

先說明一下格式約定:

1. 全域常量定義:

以字元'$'開始,全部大寫的格式。

var $MY_CONST = "ABCDEFG";

 

2. 全域函數定義:

以字元'$'開始,首字母大寫的格式。

function $A(arg) {<br /> ...<br />};

 

3. 類的定義:

使用單字首大寫的格式。

 

function MyClass {<br />};

 

4. 類內部方法及成員定義:

內部方法類似於private或protected,以一個'_'開始,第一單詞小寫,以後單字首大寫的格式。

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

 

5. 類外部方法定義:

外部方法類似於public,使用單字首大寫的格式。

 

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

 

6. 屬性存取方法定義:

屬性存取方法使用get_/set_加屬性名稱的格式,其中get_表示讀方法,set_表示寫方法。

...</p><p>get_MyProp: function() { return this._myProp; },<br />set_MyProp: function(val) { this._myProp = val; },</p><p>...

7. 檔案內部方法,不應在該檔案範圍使用:

以兩個'_'開始,首單詞小寫,以後每個單字首大寫的格式。

function __myInternalFunc(arg) {<br /> ...<br />}; // __myInternalFunc

 

 

 

 

對Object的修改

 

Exception異常類:

檔案:Exception.js

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

 

Object類的改造:

檔案:ObjectBase.js

// JScript source code</p><p>// Return class name of an object.<br />Object.prototype.get_ClassName = function() {<br /> return this._className == null ? typeof(this) : this._className;<br />};</p><p>// Set name of class, should not be called after the ctor.<br />Object.prototype.set_ClassName = function(name) {<br /> this._className = name;<br />};</p><p>// Copy members, properties and function from parent class.<br />Object.prototype.DeriveFrom= function(base) {<br /> if (arguments.length != 1 || base == null)<br /> throw new Exception(this, "DeriveFrom", "Derive from null");</p><p> if (this._ancestors == null)<br /> this._ancestors = { "Object": new Object() };</p><p> if (this._ancestors[base.get_ClassName()] != null)<br /> throw new Exception(this, "DeriveFrom", "Derive from derived class " + base.get_ClassName());</p><p> // Copy ancestors.<br /> var ancesotrs = base.get_Ancestors();<br /> for (var v in ancesotrs)<br /> if (this._ancestors[v] == null)<br /> this._ancestors[v] = ancesotrs[v];</p><p> // Derive<br /> if (this._ancestors[base.get_ClassName()] == null)<br /> this._ancestors[base.get_ClassName()] = base;</p><p> // Copy other object<br /> for (var vName in base) {<br /> if (this[vName] == null)<br /> this[vName] = base[vName];<br /> } // for<br />}; // function DeriveFrom</p><p>// Get the ancestors of an object.<br />Object.prototype.get_Ancestors = function() {<br /> if (this._ancestors != null) {<br /> var res = new Array();</p><p> for (var v in this._ancestors)<br /> res[v] = this._ancestors[v];</p><p> return res;<br /> }<br /> else<br /> return [{ "Object" : new Object()}];<br />}; // function get_Ancestors</p><p>// check if a class has an ancestor.<br />Object.prototype.HasAncestor = function(baseClassName) {<br /> if (this._ancestors == null)<br /> return false;</p><p> return this._ancestors[baseClassName] != null;<br />}; // function HasAncestor</p><p>// Return the internal _value object as string.<br />Object.prototype.ToString = function() {<br /> if (this._value != null)<br /> return this._value.toString();<br /> else<br /> return this.toString();<br />};</p><p>// Alphabet, used for $Ord, $L<br />var $ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';</p><p>// Return the char code of letter.<br />function $Ord(input, index) {<br /> switch (arguments.length) {<br /> case 0: throw new Exception(this, "$Ord", "Argument(s) needed");<br /> case 1: return input.charCodeAt(0);<br /> case 2: return input.charCodeAt(index);<br /> default: throw new Exception(this, "$Ord", "Not implemented");<br /> }<br />};</p><p>// Return the char at pos<br />function $Chr(input, index) {<br /> switch (arguments.length) {<br /> case 0: throw new Exception(this, "$Chr", "Argument(s) needed");<br /> case 1: return input.charAt(0);<br /> case 2: return input.charAt(index);<br /> default: throw new Exception(this, "$Chr", "Not implemented");<br /> }<br />};</p><p>// Return if a char is in alphabet.<br />function $L(input, index) {<br /> switch (arguments.length) {<br /> case 0: throw new Exception(this, "$L", "Argument(s) needed");<br /> case 1:<br /> {<br /> var code = $Ord(input.toUpperCase());</p><p> return code >= $Ord('A') && code <= $Ord('Z');<br /> }<br /> break;<br /> case 2:<br /> {<br /> var code = $Ord(input.toUpperCase(), index);<br /> return code >= $Ord('A') && code <= $Ord('Z');<br /> }<br /> break;<br /> default: throw new Exception(this, "$L", "Not implemented");<br /> }<br />}</p><p>// Return type of class, of course, it's a string.<br />function $T(input) {<br /> var res = input.get_ClassName();</p><p> if (res == "object")<br /> return typeof (input);<br /> else<br /> return res;<br />}</p><p>// Assert<br />function $ASSERT(option) {<br /> if (!option)<br /> alert("Failed to assert:" + option.toString());<br />}

 

SystemDebug調試輸出類:

檔案:Debug.js

// JScript source code</p><p>// ------------- Global Object --------------<br />var $Debug = null;</p><p>// ------------- Debug ----------------<br />function SystemDebug(dbgCtrl) {<br /> this._ctrl = dbgCtrl;<br />};</p><p>SystemDebug.prototype =<br />{<br /> Write: function(str) {<br /> if (this._ctrl != null && str != null) {<br /> this._ctrl.value += str;<br /> }<br /> }, // function Write</p><p> WriteLine: function(str) {<br /> if (this._ctrl != null) {<br /> if (str == null)<br /> this._ctrl.value += "/r/n";<br /> else<br /> this._ctrl.value += str + "/r/n";<br /> } // if<br /> }, // function WriteLine</p><p> Select: function() {<br /> if (this._ctrl != null) {<br /> this._ctrl.select();<br /> }<br /> }, // function Select</p><p> Enable: function(option) {<br /> if (this._ctrl != null) {<br /> this._ctrl.disabled = !option;<br /> }<br /> }, // function Enable</p><p> Enabled: function() {<br /> return this._ctrl != null ? !this._ctrl.disabled : false;<br /> }, // function Enabled</p><p> Clear: function() {<br /> if (this._ctrl != null)<br /> this._ctrl.value = "";<br /> } // function Clear<br />}; // prototype<br />

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.