HTML5学习日记2------------js类的学习

我想做的js类类似于java。单继承、多接口。

首先是生成一个借口对象。js自称是面向对象。那我们就建一个对象。所有的对象接口都会用这个对象完成。

1 var JSC = new Object();

第一步是实现一个底层类~。

 1 JSC.bottomClass = function(){
 2     //底层类的创建
 3     //create the bottom of classes
 4     var b = function(){
 5         this.initialize.apply(this,arguments);
 6         return this;
 7     };
 8     b.prototype.__fuc__ = new Object;
 9     b.prototype.__var__ = new Object;
10     b.prototype.__fuc__.initialize = function(){};
11     b.prototype.initialize = b.prototype.__fuc__.initialize;
12     b.prototype.__var__.__parent__ = undefined;
13     b.prototype.__parent__ = b.prototype.__var__.__parent__;
14     b.prototype.__var__.__type__ = "Class";
15     b.prototype.__type__ = b.prototype.__var__.__type__;
16     b.prototype.__fuc__.getSuper = function(){
17         return this.__var__.__parent__;
18     };
19     b.prototype.__var__.__name__ = "Class_" + JSC.guidGenerator("S");
20     b.prototype.__name__ = b.prototype.__var__.__name__;
21     b.prototype.getSuper = b.prototype.__fuc__.getSuper;
22     return b;
23 };

底层类类似与java的底层Object对象。底层类其实并不对外开放。开发时没必要调用他。

第二步是实现创建一个类对象。

 1 JSC.createClass = function(a){
 2     //类的创建
 3     //create a class object
 4     var t = JSC.extend(JSC.bottomClass());
 5     //create the functions of the class 
 6     for(var v in a){
 7         if(typeof a[v] !== 'function') {
 8             t.prototype.__var__[v] = a[v];
 9             t.prototype[v] = t.prototype.__var__[v];
10         } else {
11             t.prototype.__fuc__[v] = a[v];
12             t.prototype[v] = t.prototype.__fuc__[v];
13         }
14     }
15     t.prototype.__var__.__name__ = "Class_" + JSC.guidGenerator("S");
16     t.prototype.__name__ = t.prototype.__var__.__name__;
17     return t;
18 };

这是主要函数。调用此函数来生成我们的类。首先他会生成一个继承于底层类的类。然后把a所有的变量和函数加入到该类中。

第三步是继承函数。

 1 JSC.extend = function(a, b) {
 2     //类的继承
 3     //extend need a class
 4     if(!JSC.isJSClass(a)) {
 5         this.developalert("The parent object not a jsc class");
 6         return null;
 7     }
 8     //create a child class
 9     var c = JSC.clone(a);
10     c.prototype.__var__.__parent__ = a.prototype;
11     c.prototype.__parent__ = c.prototype.__var__.__parent__;
12     for(var v in b){
13         if(typeof b[v] !== 'function') {
14             c.prototype.__var__[v] = b[v];
15             c.prototype[v] = c.prototype.__var__[v];
16         } else {
17             c.prototype.__fuc__[v] = b[v];
18             c.prototype[v] = c.prototype.__fuc__[v];
19         }
20     }
21     c.prototype.__var__.__name__ = "Class_" + JSC.guidGenerator("S");
22     c.prototype.__name__ = c.prototype.__var__.__name__;
23     return c;
24 };

这是第二个主要函数。生成一个继承于a的类。然后把b的函数和变量加入到该类中。

第四步是克隆函数。

 1 JSC.clone = function(a) {
 2     //clone a class
 3     if(!JSC.isJSClass(a)) {
 4         this.developalert("a not a jsc class");
 5         return null;
 6     }
 7     var c = JSC.bottomClass();
 8     for(var fuc in a.prototype.__fuc__){
 9         c.prototype.__fuc__[fuc] = a.prototype.__fuc__[fuc];
10         c.prototype[fuc] = c.prototype.__fuc__[fuc];
11     }
12     for(var v in a.prototype.__var__){
13         c.prototype.__var__[v] = a.prototype.__var__[v];
14         c.prototype[v] = c.prototype.__var__[v];
15     }
16     return c;
17 };

这函数也不是对外开放的函数。这是根据js特性而写的。在继承函数中不能写var c = a; 为什么?这。。。

第五步是判断函数。

1 JSC.isJSClass = function(a) {
2     if(typeof a!=='function' && a.prototype.__var__ === undefined && a.prototype.__fuc__ === undefined && a.prototype.__var__.__type__ !== 'Class') {
3         return false;
4     }
5     return true;
6 }
7  

这也是不对外开发的函数。判断a是否是由JSC生成创建出来的。仲感觉这么判断还是有很多漏洞。

最后是一个工具。

JSC.guidGenerator = function(type,split){
    var C4 = function () {
        //创建4位序列
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    };
    ttype = type;
    var a = typeof ttype;
    if(split !== 1) splitStr = "_";
    else splitStr = "-";
    if(typeof ttype !== "string") ttype = "D";
    switch(ttype.toLocaleUpperCase()){
    case "D":
        return (C4() + C4() + splitStr + C4() + splitStr + C4() + splitStr + C4() + splitStr + C4() + C4() + C4());
    case "S":
        return (C4() + C4() + C4() + C4() + C4() + C4() + C4() + C4());
    case "T":
        return (C4() + C4() + splitStr + C4() + C4() + splitStr + C4() + C4() + splitStr + C4() + C4());
    }
};

js文件:https://files.cnblogs.com/loveTechn/class.js

测试实例:https://files.cnblogs.com/loveTechn/test_myClass.js

下一步是对生成的实例进行操作。我发现有些操作是在生成实例之后的。

posted on 2013-07-16 12:23  loveTechn  阅读(1065)  评论(0编辑  收藏  举报

导航