javascript基于Base.js的父类重载

本文介绍Base.js库的基础使用方法,并通过具体实例演示如何利用Base.js进行对象继承,实现Person与User对象的创建及调用。

1. 下载Base.js库

Base.js

---------------------------

/*
    Base.js, version 1.1a
    Copyright 2006-2010, Dean Edwards
    License: http://www.opensource.org/licenses/mit-license.php
*/

var Base = function() {
    // dummy
};

Base.extend = function(_instance, _static) { // subclass
    var extend = Base.prototype.extend;
    
    // build the prototype
    Base._prototyping = true;
    var proto = new this;
    extend.call(proto, _instance);
  proto.base = function() {
    // call this method from any other method to invoke that method's ancestor
  };
    delete Base._prototyping;
    
    // create the wrapper for the constructor function
    //var constructor = proto.constructor.valueOf(); //-dean
    var constructor = proto.constructor;
    var klass = proto.constructor = function() {
        if (!Base._prototyping) {
            if (this._constructing || this.constructor == klass) { // instantiation
                this._constructing = true;
                constructor.apply(this, arguments);
                delete this._constructing;
            } else if (arguments[0] != null) { // casting
                return (arguments[0].extend || extend).call(arguments[0], proto);
            }
        }
    };
    
    // build the class interface
    klass.ancestor = this;
    klass.extend = this.extend;
    klass.forEach = this.forEach;
    klass.implement = this.implement;
    klass.prototype = proto;
    klass.toString = this.toString;
    klass.valueOf = function(type) {
        //return (type == "object") ? klass : constructor; //-dean
        return (type == "object") ? klass : constructor.valueOf();
    };
    extend.call(klass, _static);
    // class initialisation
    if (typeof klass.init == "function") klass.init();
    return klass;
};

Base.prototype = {    
    extend: function(source, value) {
        if (arguments.length > 1) { // extending with a name/value pair
            var ancestor = this[source];
            if (ancestor && (typeof value == "function") && // overriding a method?
                // the valueOf() comparison is to avoid circular references
                (!ancestor.valueOf || ancestor.valueOf() != value.valueOf()) &&
                /\bbase\b/.test(value)) {
                // get the underlying method
                var method = value.valueOf();
                // override
                value = function() {
                    var previous = this.base || Base.prototype.base;
                    this.base = ancestor;
                    var returnValue = method.apply(this, arguments);
                    this.base = previous;
                    return returnValue;
                };
                // point to the underlying method
                value.valueOf = function(type) {
                    return (type == "object") ? value : method;
                };
                value.toString = Base.toString;
            }
            this[source] = value;
        } else if (source) { // extending with an object literal
            var extend = Base.prototype.extend;
            // if this object has a customised extend method then use it
            if (!Base._prototyping && typeof this != "function") {
                extend = this.extend || extend;
            }
            var proto = {toSource: null};
            // do the "toString" and other methods manually
            var hidden = ["constructor", "toString", "valueOf"];
            // if we are prototyping then include the constructor
            var i = Base._prototyping ? 0 : 1;
            while (key = hidden[i++]) {
                if (source[key] != proto[key]) {
                    extend.call(this, key, source[key]);

                }
            }
            // copy each of the source object's properties to this object
            for (var key in source) {
                if (!proto[key]) extend.call(this, key, source[key]);
            }
        }
        return this;
    }
};

// initialise
Base = Base.extend({
    constructor: function() {
        this.extend(arguments[0]);
    }
}, {
    ancestor: Object,
    version: "1.1",
    
    forEach: function(object, block, context) {
        for (var key in object) {
            if (this.prototype[key] === undefined) {
                block.call(context, object[key], key, object);
            }
        }
    },
        
    implement: function() {
        for (var i = 0; i < arguments.length; i++) {
            if (typeof arguments[i] == "function") {
                // if it's a function, call it
                arguments[i](this.prototype);
            } else {
                // add the interface using the extend method
                this.prototype.extend(arguments[i]);
            }
        }
        return this;
    },
    
    toString: function() {
        return String(this.valueOf());
    }
});


-------------------------

test.html

-------------------------

<html>
<head>
<title>javascript</title>
<script type="text/javascript" src="Base.js"></script>
<script type="text/javascript">
    //基于Base库(通过引用Base.js实现)创建Person对象

   //.extend()接受的参数是一个简单对象,该对象含有简单属性和值
    var Person=Base.extend({
        constructor:function(name){
            this.name=name;
        },
        getName:function(){
            return this.name;
        }
    });
    //继承Person对象
    var User = Person.extend({
        constructor:function(name,password){
            this.base(name);
            this.password=password;
        },
        getPassword:function(){
            return this.password;
        }
    });
    
    function testCallFn(){
        var p = new Person("niplo");
        alert(p.getName());
        var u = new User("testUser","123456");
        alert(u.getName()+" "+u.getPassword());
        var u2 = new User("spider");
        alert(u2.getName());
    }
</script>
</head>
<body>

<input type="button" value="测试基于Base库继承对象" onclick="testCallFn();" /><br />
</body>
</html>



内容概要:本文详细评测了2025年七款主流AI编程工具,分别是Cursor、Claude 3.7 Opus、GitHub Copilot Pro、ChatGPT-4.1、DeepSeek V3、Windsurf(原Codeium)和Trae。文章首先介绍了AI编程工具的背景及其对编程领域的变革意义,随后从代码生成能力、智能补全实力、代码分析与优化、多语言支持广度、与IDE集成度等多个维度对比各工具的功能。接着通过Web应用开发、数据分析项目、小型游戏开发等实战案例,验证了各工具的实际应用效果。最后分析了各工具的成本与收益,并结合用户反馈给出了综合评分和排名,为开发者选择合适工具提供了参考。适合人群:具备一定编程基础的开发者,包括编程新手、全栈工程师、资深开发者和架构师。使用场景及目标:①帮助开发者选择适合自身需求的AI编程工具;②提升开发效率,减少手动编写代码的工作量;③在复杂项目开发中提供智能代码生成、补全和优化功能;④支持多种编程语言和开发环境,适应不同类型的开发任务。其他说明:文章还展望了AI编程工具的未来发展,指出它们将更加智能化、多功能化,并与云计算、大数据、区块链等技术深度融合,为开发者提供更便捷、高效的编程体验。此外,AI编程工具将更加注重用户体验和个性化定制,帮助不同水平的开发者快速上手并提高生产力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值