javascript封装

var GBGIS_GRAPHICS =function () {
    function wrapeShape(obj, type, srid, body) {
        obj.type = type;
        obj.srid = srid;
        obj.body = body;
        obj.writeXML = function (xmlDoc, xmlNode) {
            var shape = xmlDoc.createElement_x(TAG_NAME_SHAPE);
            shape.setAttribute(TAG_NAME_TYPE, this.type);
            shape.setAttribute(TAG_NAME_SRID, this.srid);
            xmlNode.appendChild(shape);
            shape.textContent = this.body2s();
        }
        return obj;
    

    return {
        newGraphic: function (id, shape, symbol) {
            return {
                id: id, shape: shape, symbol: symbol
            };
        }
    };
} ();

return{}中的函数和属性相当于 public,其它则为private

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

//方式一:使用方式相当于静态类,特点:使用时不用初始化,使用方便,适合做常量的集合

JScript1.js:
var staticClass = {
    fun: function () {
        alert("fun");
    },
    name: "const name"
};

test.html:

<script src="JScript1.js" type="text/javascript"></script>

<script type="text/javascript">

staticClass.fun();

</script>

*******************************************************************************

方式二:同一般类,可以有初始化参数
JScript1.js:

function conmonClass(par) {
    var pre = par;
    this.show = function () {
        alert("默认参数" + pre);
     };
};
 test.html:

<script src="JScript1.js" type="text/javascript"></script>

<script type="text/javascript">

    var con = new conmonClass("初始化");
        con.show();

 </script>

*******************************************

//可以直接调用的方法
JScript1.js:

this.show = function (par) {
    alert(par);
};

  test.html:

show("show");

*********************************************************************************

//利用call实现类的继承

JScript1.js:

function classA(sColor){ 
   this.color=sColor; 
   this.sayColor=function(){ 
     alert(this.color); 
  
};  

function classC(sColor,sName){ 
   classA.call(this,sColor); 
   this.name=sName; 
   this.sayName=function(){ 
     alert(this.name); 
   }
};

  test.html:

<script src="JScript1.js" type="text/javascript"></script>

<script type="text/javascript">

   var obj1 = new classC("red", "jack");

        obj1.sayColor();
        obj1.sayName(); 

 </script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值