首页新闻找找看学习计划

关于基于underscore库写的一个基类问题

0
悬赏园豆:10 [待解决问题]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>scp.core测试</title>      
    <script src="underscore.js"></script>    
</head>
<body>
<script type="text/javascript">
//------------------------------------------------------------
(function(){
    var global = this,Scp={},Base;
    global.Scp=Scp;
    Base = Scp.Base = function(){
    }
    //构造函数
    makeCtor = function(){
        function constructor () {
            return this.constructor.apply(this, arguments) || null;
        }
        return constructor;
    }
    _.extend(Scp.Base,{
        extend:function(parent){
            var parentPrototype = parent.prototype;            
            this.prototype = parentPrototype;
            this.superclass = this.prototype.superclass = parent; 
            this.self = this.prototype;
        },
        addMembers:function(members){
            var prototype = this.prototype,
                names = [],
                i, ln, name, member,clsName = prototype.$classname;
                for (name in members) {
                    names.push(name);
                }
                for (i = 0,ln = names.length; i < ln; i++) {
                    name = names[i];
                    if (members.hasOwnProperty(name)) {
                        member = members[name];
                        if (typeof member == 'function' ) {
                            member.$ownerClsName = clsName;
                            member.$owner = this;
                            member.$name = name;
                        }
                        prototype[name] = member;
                    }
                }                
                return this;
        },
        callParent:function(args){
            var method;//????this.callParent.caller?这个域变成了 Scp.define.constructor
            method = this.callParent.caller;
            method = method.$owner.superclass[method.$name];
            return method&&(method).apply(this, args || null);
        }
    });
    Base.addMembers.call(Base,Base);//
    _.extend(Scp,{
            ns : function (name) {
                var names = name.split('.');
                var parent = window;
                for (var i = 0; i < names.length; i++) {
                    var n = names[i];
                    var p = parent[n];
                    if (p === undefined) {
                        parent[n] = p = {};
                    }
                    parent = p;
                }
                return parent;
            },
            define:function(ns,data){
                var cls = makeCtor(),parent;
                cls = Scp.setNs(ns,cls);//制造构造函数
                _.extend(cls,Base);
                if(data.extend){
                    parent = Scp.ns(data.extend);
                    delete data.extend;
                    Base.extend.call(cls,parent);
                    //cls.extend(parent);
                }else{ 
                    //cls.extend(Base);
                    Base.extend.call(cls,Base);
                }
                data.$classname = ns;
                _.extend(cls.prototype,data);//继承
                Base.addMembers.call(cls,cls.prototype);//修辞一下方法 
                return cls;
            },
            create:function(ns,data){
                var cls = Scp.ns(ns);
                return new cls(data);
            },
            setNs: function(name, value) {
                var root = global,
                    parts = name.split('.'),
                    ln = parts.length - 1,
                    leaf = parts[ln],
                    i, part;

                for (i = 0; i < ln; i++) {
                    part = parts[i];
                    if (typeof part != 'string') {
                        root = part;
                    } else {
                        if (!root[part]) {
                            root[part] = {};
                        }
                        root = root[part];
                    }
                }
                root[leaf] = value;
                return root[leaf];
            }
        }
    );
}).call(this);

window.onload = function(){
    debugger;
    Scp.define('Scp.Component',{
        renderTpl:'',
        constructor:function(){
            this.initComponent();
        },
        initComponent:function(){
            alert(1);
        }
    });
    Scp.define('Scp.Menu',{
        extend:'Scp.Component',
        renderTpl:'asdfasdf',
        constructor:function(config){
            this.callParent(arguments);
        },
        initComponent:function(){

        }
    });
    window.menu = new Scp.Menu({a:1});
}
</script>
</body> 
</html>

当调用 callParent一直报错。大侠急救我一下。。。在线等待。。。

Ailen的主页 Ailen | 初学一级 | 园豆:177
提问于:2013-05-27 11:57
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册