首页 > 编程语言 >adm-jclass: 一个面向对象编程的js库

adm-jclass: 一个面向对象编程的js库

时间:2023-06-04 14:31:43浏览次数:39  
标签:function total name jclass namespace 面向对象编程 adm jClass class


[url]https://code.google.com/p/adm-jclass/[/url]:
A javascript library that allows highly object-oriented application programming in your browser.

介绍: [url]https://code.google.com/p/adm-jclass/wiki/jClassDocumentation[/url]
jClass is a Java Script library that allows you to write highly object-oriented code.

[b]Introduction[/b]
jClass is a JavaScript a javascript library that allows highly object-oriented application programming in your browser.

[color=red][b]jClass[/b][/color]
jClass is the base Object all classes extend from and can inherit other jClass Objects and/or implement JInterface objects.

The base jClass Object contains the following properties/methods

[b]Name Type/Return Type Description[/b]
[b]_prototype[/b] Object, An object describing the class
[b]_prototype.typeName[/b] String, Name of the current class type
[b]_prototype.instanceId[/b] Number, Unique class identifier
[b]_prototype.implementing[/b] Array, An array of interface ID's the class is implementing
[b]_prototype.extending[/b] Array, An array of class ID's the class is inheriting from
[b]isImplementationOf(jInterface)[/b] Boolean Returns true if the class is implementing the given jInterface object
[b]isInstanceOf(jClass)[/b] Boolean, Returns true if the class is inheriting from the given jClass object
Thing to consider: When you create a jClass that both extends another class and implements an interface, the order must be

var MyClass = jClass.extend(MyBaseClass).implement(MyInterface)({ .... })


When you extend a class, the subclass inherits all of the public members from the parent class. Unless a class overrides those methods, they will retain their original functionality.



var BaseClass = jClass({
    public : {
       add : function(x,y) {
          total = x + y;
          return total;
       },
       total : 0
    }
})

var MyClass = jClass.extend(BaseClass)({
    public : {
       add : function(x,y) {   // overiding a parent function
           _super.add(x,y);    // _super.add refers to the class defined in the parent
           return this.total;  // total is available by inheritance 
       }
    }
})




[color=red][b]jInterface[/b][/color]


Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled. Defining a body for the methods here will throw an error.



All methods declared in an interface must be public, this is the nature of an interface.



You can also declare properties in the interface and not only functions



// defining interface

var IMyInterface = jInterface({
    someFunction : function() {},
    someOtherFunction : function(param1, param2) {},

    name : "",
    someId : 0
});

// implementing interface

var MyClass = jClass.implement(IMyInterface)({
   public : {
      someFunction : function() {
         // method body
      },
      someOtherFunction : function(param1, param2) {
         // method body
      },

      name : "Hello World!",
      someId : Math.rand()
   }
})




[color=red][b]Keywords and Access Modifiers[/b][/color]


A jClass object can have 4 base blocks interpreted by the jClass compiler.



[b]constructor [/b](function) - a function that will be called when the class is instantiated (can be overloaded)


[b]public [/b](object) - public properties and methods available outside the class.


[b]private [/b](object) - private properties and methods available only inside the class.


[b]static [/b](object) - static properties and methods available without instantiating the class


var MyClass = jClass({
     constructor : function(arguments, ...) {
        // ....
     },
     public : { 
        im_static_too : Static(function() { .... }),
        // ....
     },
     private : {
        // ....
     },
     static : {
        // ....
     },
     SomeFunction : function() { ... },
     SomeObject : { ... },
     SomeProperty  : "..."
  })


Defining anything outside these blocks (constructor, public, private, static) is automatically moved in the public block.



You can also define a static member by wrapping it in [b]Static()[/b] (like in the example above, im_static_too() becomes static).



If you wish to define more function with different number of parameters but they all must have the same name, you can use the [b]Overload()[/b] function.



find : Overload(function(name) { ... }, function(name, age) { ... })

   ....

   obj.find('john'); // calls the first function and finds by name
   obj.find('john', 20); // calls the second function and finds by name and age




[color=red][b]Setters and Getters[/b][/color]


If you wish to define a property but call a function whenever that value is changed or read, you can define it using GetSet(), Get(), Set() and it will behave just like any other variable except your functions will be responsible of the actual value being written or read.



tmp : GetSet(
          function() { // first method is the getter and should return the value
             // do someting before returning the value
             return _tmp;
          },
          function(val) { // second method is the setter and must set the value
             // do someting (validations, notifications, etc) with the value being set
             _tmp = val;
          }),
   count : Get(function() { // defining only a setter
              return _count;
           }),
   total : Set(function(val) { // defining only a setter
              _total = _total + val;
           })
   ....



  x.tmp = 2; // calls the setter with parameter 2
  x.tmp;     // calls the getter 

  x.count;      // calls the getter
  x.count = 3;  // no setter defined, this is Read-Only and will throw an error

  x.total = 10; // calls the setter
  x.total;      // no getter defined, this will return undefined



When you create a property and you make it a getter or a setter (or both), a private variable is automatically created and the name of that property will be the name you give it preceded by an underscore



myvar : Get(function() {
           return _myvar; // _myvar is created by the compiler as a private variable
        });



Getters and Setters are defined using defineSetter prototype method available inside objects. Unfortunately, IE does not provide such a functionality and getters and setters are not available as described above. Until I find a method to make this work, getters and setters are available in IE as such:



total : GetSet(function() { return _total; }, function(val) { _total = val; });

   obj.getTotal();  // calls getter
   obj.setTotal(1); // calls setter




[b][color=red]Other core functions[/color][/b]


[b]namespace(object)[/b]


In general, a namespace is an abstract container providing context for the items, in JavaScript, a namespace is an object, containing all the methods and objects you define inside without worrying that you may override others with same name



namespace("my.namespace")({
       myFunction : function() { return 1; };
  });


  namespace("other.namespace")({
       myFunction : function() { return 2; };
  });

  my.namespace.myFucntion(); // 1
  other.namespace.myFunction(); // 2



[b]include(file or namespace, ....)[/b]


jClass built in include/require functions allow you to load external files and/or libraries at runtime.



include("some/jsfile.js", "some.name.space", ....);


Including namespaces you are required to respect a naming convention for your files and the place they are stored.



For example, if you create the namespace "[b]my.namespace[/b]" and wish to include the namespace by name, the file name must be "[b]namespace.js[/b]" and be located in "[b]/libraries/my/namespace.js[/b]"



That "[b]/libraries/[/b]" there is the folder where the libraries are located and has the default value of "/libraries/". If you wish to change this you can use another built in iniset function and change the LIB setting to wherever your libraries are lcoated



iniset("LIB", "/path/to/js/libraries/");
   console.log(initget("LIB")); // "/path/to/js/libraries/"

   include("my.namespace"); // will load /path/to/js/libraries/my/namespace.js


Note! - The libraries 'repository' is only prepended when you include namespaces. It will not affect the path of a included file (Ex include("path/file.js"); will load path/file.js)



[b]initset(name, value)[/b]


iniset() allows you to change some configuration variables that jClass uses internally. You can of course edit the jClass.js by hand but if you wish to use the minified version you may brake something.



A usage example can be seen above.



[b]initget(name)[/b]


iniget() retrieves the current value of a jClass setting


console.log(iniget("LIB")); // returns "/libraries"/




IE


A boolean value indicating if user browser is Internet Explorer




[b][color=red]You can use jClass to:[/color][/b]



Define namespaces


Create and extend other classes


Create and implement interfaces


Define public/private/static/override/overload methods


Define getter and setter methods


The basic syntax for jClass is:



include("some.other.files");

namespace("my.namespace")
({
    MyClass : jClass.extends(BaseClass).implements(SomeInterf, SomeOtherInterf) 
    ({
        constructor : function(){ 
         .... 
        },
        private : {
          ....
        },
        public : {
          ....
        },
        static : {
          ....
        }
    }),
    OtherClass : jClass({
       ......
    })
})


A set of really useful libraries are also included. You can check them out in the Wiki



And a demonstration example:



File : libraries/adm/classes.js



namespace("adm.classes")
({
     // Defining a interface. Implement bodies for the methods here will throw an error
    ITestInterface = jInterface({
        add : function(name) {}
        remove : function() {}
    }),

    // Defining a class to extend later
    BaseClass = jClass({
        public : {
            add : function(name) {
                users.push(name);
            } 
            getUsers: function() {
                // returns reference to private variable users
                return users; 
            }
        },
        private : {
            // private variable, no one except this class can access this
            users : ['John', 'Bill']
        },
        static : {
            BASE_CLASS_STATIC : "i'm from base class"
        }
    })
});


File : index.html



include("adm.classes");

var MainClass = jClass.extend(adm.classes.BaseClass).implement(adm.classes.ITestInterface)({
     constructor : function() {
           // class constructed
           localCopy = _super.getUsers();
     },
     public : {
         // Not implementing this will throw and error because it's required by the interface
         remove : Overload(
                     function(name) { // delete user by name  
                         delete localCopy[localCopy.indexOf[name]];
                     },
                     // overloading function remove to delete users between start and end positions
                     function(start, end) {
                         localCopy.splice(start, end);
                     }
                  ),
          // Overiding baseclass function add()
          add : function(name) {
              _super.add(name);
              this.doSometing();
          },
          doDomething : function() {
               // does something
          },
          total : GetSet(function() {
                    return _total; // set total
                  },
                  function(val) {
                     _total = val; // get total 
                     this.doSomething();
                  })
     }, 
     private : {
        localCopy : []
     },
     static : {
        SOMETHING_STATIC : 123
     }
});

var obj = new MainClass();

obj.isInstanceOf(adm.classes.MainClass); // true
obj.isInstanceOf(BaseClass); // true
obj.isImplmentationOf(adm.classes.ITestInterface); // true

MainClass.SOMETHING_STATIC; // 123
MainClass.BASE_CLASS_STATIC; // inherited from base class "i'm from base class"

obj.total = 1; // calling setter: set's new value to _total and calls doSomething()
obj.total; // calling getter: returns value of _total (1)

obj.add('Tom'); // adds user Tom

obj.remove('Bill'); // removes user by name
obj.remove(0, 3); // removes users between 0-3

标签:function,total,name,jclass,namespace,面向对象编程,adm,jClass,class
From: https://blog.51cto.com/u_3871599/6410628

相关文章

  • 靳宇灵 | FastAdmin多语言配置
    在FastAdmin中可以在任何位置(控制器、视图、JS)使用__('语言标识');调用语言包,如果语言标识不存在,则直接输出该语言标识。使用方法FastAdmin中的__函数和ThinkPHP中的lang函数在传参上有些许区别比如__('Mynameis%s',"FastAdmin");将会返回MynameisFastAdmin而如果采用Thin......
  • fastadmin隐藏搜索按钮
     设置showSearch:false即可 ......
  • fastadmin隐藏右上角的搜索按钮及其导出等
     想要隐藏这些按钮,有两种方式一、直接html文件里修改data-show-export="false"data-show-toggle="false"data-show-columns="false"data-search="false"data-common-Search="false" 二、在bootstarpTable初始化的时候设定//初始化表格......
  • vue-element-admin安装依赖报错问题
    vue-element-admin安装依赖的时候报以下错误npmERR!codeENOENTnpmERR!syscallspawngitnpmERR!pathgitnpmERR!errno-4058npmERR!enoentAnunknowngiterroroccurrednpmERR!enoentThisisrelatedtonpmnotbeingabletofindafilenpmERR!enoentnpmE......
  • Readme使用手册
    title:Readme工具使用手册categories:-手册目录ReadMe用法npx用法Windows系统使用ReadMereadme是一个自动生成readme的工具开源项目地址:https://github.com/kefranabg/readme-md-generator开源项目作者:FranckAbgrall用法npx用法Makesureyouhavenpxi......
  • Day06-面向对象编程
    面向对象编程(OOP)面向对象编程的本质:以类的方式组织代码,以对象的形式组织(封装)数据抽象三大特性封装继承多态非静态方法之间可以互相调用不用new对象,但是在静态方法里面无法直接调用非静态方法,原因是静态方法是和类一起加载的,非静态方法是在类实例化之后才存在的,相当......
  • npm i命令报the command again as root/Administrato
    首先在确保自己node版本和np版本是符合的情况下:对照https://nodejs.org/zh-cn/download/releases/,查看node和npm相对的版本报错代码4048一般情况都是管理权限不够可以win10系统中搜素PowerShell,右键以管理员身份运行,打开后执行命令:set-ExecutionPolicyRemoteSigned,更改权限为A......
  • 前端学习路线图roadmap(转)
    前端学习路线这份学习路线并不完美,也不会有最终形态,正如前端不可预见、永无止境的未来。......
  • 负载均衡集群ipvsadm命令及基本用法
     ipvsadm是LVS在应用层的管理命令,我们可以通过这个命令去管理LVS的配置。需要使用yum单独安装。基本用法:ipvsadmCOMMAND[protocol]service-address        [scheduling-method][persistenceoptions]ipvsadmcommand[protocol]service-address  ......
  • SimpleAdmin手摸手教学之:基于Ant Design Tree组件实现树形结构数据的异步加载
    一、说明当有一个树形结构的数据有非常多个节点的时候,一次性加载所有节点会显得过于臃肿,可能会对性能造成影响,正好AntDesign的树(Tree)组件支持异步加载,于是我就想把异步加载封装为一个组件,可以减少接口数据返回,点击展开节点,动态加载数据。非常好用!二、前端实现需要接收一些......