JavaScript-based formula interpreter-2 [improving your type system]

Source: Internet
Author: User
Tags constant definition uppercase letter

Here we will transform the object at the beginning of everything to form our own type system to facilitate subsequent types implementation, recognition, and processing.

 

Format conventions

First, describe the format conventions:

1. Global constant definition:

Start with '$', in uppercase.

Var $ MY_CONST = "ABCDEFG ";

 

2. Global Function Definition:

Start with '$' and start with an uppercase letter.

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

 

3. Class Definition:

Use the uppercase format of the first letter of a word.

 

Function MyClass {<br/> };

 

4. Class internal methods and member definitions:

The internal method is similar to private or protected. It starts with a '_', the first word is in lowercase, And the last letter is in uppercase format.

Function MyClass () {<br/>... <br/> this. _ myFunc = function (arg) {<br/>... <br/>}; // MyClass

 

5. Class external method definition:

The external method is similar to public and uses the uppercase format of the first letter of a word.

 

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

 

6. Attribute access method definition:

The attribute access method uses get _/SET _ and the attribute name format. Get _ indicates the read method, and set _ indicates the write method.

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

7. File internal methods should not be used in the file scope:

It starts with two '_' and the first word is in lowercase, and then the first letter of each word is in uppercase format.

Function _ myInternalFunc (arg) {<br/>... <br/>}; // _ myInternalFunc

 

 

 

 

Object Modification

 

Exception:

File: exception. js

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

 

Object Class Transformation:

File: 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, shocould 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_classnm E ()] = 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 = 'abcdefghijklmnopqrstuvwxy '; </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/> cas E 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/>}</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); <B R/> else <br/> return res; <br/>}</p> <p> // Assert <br/> function $ ASSERT (option) {<br/> if (! Option) <br/> alert ("Failed to assert:" + option. toString (); <br/>}

 

Systemdebug debugging output class:

File: 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/>

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.