Google Map 类实例在类式继承中的实现

众所周知,程序的实现不可能会是完美的。下面是google Map类在继承实现的写法。首先是照抄《JavaScript设计模式》中的类式继承:

function extend(subClass, superClass) {
	function F() {}
	F.prototype = superClass.prototype;
	subClass.prototype = new F();
	subClass.prototype.constructor = subClass;
    
    subClass.superclass = superClass.prototype;
    if (superClass.prototype.constructor == Object.prototype.constructor) {
        superClass.prototype.constructor = superClass;
    }
}
function SubMap(elm, config) {
    console.log(SubMap.superclass.constructor);
    SubMap.superclass.constructor.call(this, elm, config);
    
}
extend(SubMap, google.maps.Map);
var mapObj1 = new SubMap($('#map_Box')[0], {
       zoom: 13,
       center: new google.maps.LatLng(31.227, 121.519),
       mapTypeId: google.maps.MapTypeId.ROADMAP
});

上面的google地图在页面中显示不了,说明代码是有问题的。下面是对上面实现的改写:

function extend(subClass, superClass) {
	function F() {}
	F.prototype = superClass.prototype;
	subClass.prototype = new F();
	subClass.prototype.constructor = subClass;
    
    subClass.superclass = superClass;
}
function SubMap(elm, config) {
    SubMap.superclass.call(this, elm, config);
}
extend(SubMap, google.maps.Map);

var mapObj1 = new SubMap($('#map_Box')[0], {
       zoom: 13,
       center: new google.maps.LatLng(31.227, 121.519),
       mapTypeId: google.maps.MapTypeId.ROADMAP
});

嗯,这种继承实现的写法google地图在页面中显示得很好。

(完)

转载于:https://www.cnblogs.com/georgewing/archive/2011/04/07/1987534.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值