随笔-183  评论-160  文章-1 

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地图在页面中显示得很好。

(完)

posted on 2011-04-07 17:42 George Wing 阅读(...) 评论(...) 编辑 收藏