简单js组件、库实现

本文介绍了一个简单的JavaScript组件CObject的实现,包括命名空间、全局变量、构造函数、私有与公有属性和方法的定义。CObject组件具备版本信息、ID属性,并提供了访问私有方法的特权方法。此外,还展示了如何使用静态属性和方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(function()
{
    /*################################命名空间开始###########################*/
    /*============================全局变量定义区开始=========================*/
    //此范围内定义的变量与外部同名变量不冲突   
    var versioninfo =
    {
        /*主版本号 . 子版本号 [ 修正版本号 [. 编译版本号 ]]*/
        Version: { Major: "0", Minor: "1", Revision: "5", Build: "alpha" },
        Author: "hawkwolf",

        Email: "zhandx@163.com",
        Date: "2009-12-16",
        getVersion: function() { return versioninfo.Version.Major + "." + versioninfo.Version.Minor + "." + versioninfo.Version.Revision + "." + versioninfo.Build }
    };
    var spaceLength = 50; //图元间距长度   
    /*============================全局变量定义区结束=========================*/
    if (window.CObject == null)
    {
        /*****************************CObject组件开始***************************/
        window.CObject = function(id)
        {
            Init(); //这里先调用了私有方法Init

            /*----------------------------属性定义区开始-------------------------*/
            /*****************************私有属性定义区开始**************************/
            var other = "CObject";
            var owner = this;
            /*****************************私有属性定义区结束**************************/

            /*****************************公有属性定义区开始**************************/
            this.name = "CObject";
            this.ID = id;
            /*****************************公有属性定义区结束**************************/

            /*****************************静态属性定义区开始**************************/
            CObject.Counter++;   //静态计数器                       
            /*****************************静态属性定义区结束**************************/

            /*----------------------------属性定义区结束-------------------------*/

            /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@私有方法定义区开始@@@@@@@@@@@@@@@@@@@@@@@@@*/
            function Init()
            {
                alert("at here is Init Code!");
            };
            function privateMethod()
            {
                alert('Invoke private method! output private variable other is "' + other.toString() + '", public variable ID is "' + this.ID.toString() + '", global variable version is "' + versioninfo.getVersion().toString() + '"');
            };
            function privateMethod1(msg)
            {
                alert(msg);
            };
            /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@私有方法定义区结束@@@@@@@@@@@@@@@@@@@@@@@@@*/

            /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@特权方法定义区开始@@@@@@@@@@@@@@@@@@@@@@@@@*/
            this.privilegeMethod = function()//在最后面添加
            {
                alert('Invoke privilage method! output private variable other is "' + other.toString() + '", public variable ID is "' + this.ID.toString() + '", global variable version is "' + versioninfo.getVersion().toString() + '"');
                privateMethod1("this is privilage method invoke private method!");
            };
            this.privilegeMethod1 = function()//在最后面添加
            {
                alert('Invoke privilage method1! output private variable other is "' + other.toString() + '", public variable ID is "' + this.ID.toString() + '", global variable version is "' + versioninfo.getVersion().toString() + '"');
                privateMethod1("this is privilage method invoke private method!");
            };
            /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@特权方法定义区结束@@@@@@@@@@@@@@@@@@@@@@@@@*/
        };

        /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@公有方法定义区开始@@@@@@@@@@@@@@@@@@@@@@@@@*/
        CObject.prototype = {
            getVersion: function()
            {
                //此公有方法如果写在构造函数外,那么访问other将不会成功!这里成功是因为此方法和other作用于相同缘故。
                alert('Invoke public method! output public variable ID is "' + this.ID.toString() + '", global variable version is "' + versioninfo.getVersion().toString() + '"');
            },
            toString: function()
            {
                alert('"CObject"');
            },
            getType: function()
            {
                alert('"CObject"');
            },
            getOhter: function()//这里访问other会失败,不在作用域内的缘故
            {
                alert('Invoke public method! output private variable other is "' + other.toString() + '", public variable ID is "' + this.ID.toString() + '", global variable version is "' + versioninfo.getVersion().toString() + '"');
            }
        };

        /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@公有方法定义区结束@@@@@@@@@@@@@@@@@@@@@@@@@*/

        /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@静态方法定义区开始@@@@@@@@@@@@@@@@@@@@@@@@@*/
        CObject.getCounter = function()
        {
            return CObject.Counter;
        };
        /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@静态方法定义区结束@@@@@@@@@@@@@@@@@@@@@@@@@*/
        /*****************************CObject组件结束***************************/
    }
    /*################################命名空间结束###########################*/
})();

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值