日期:2014-05-16  浏览次数:20344 次

各位 帮忙设计一下类结构吧
地图上存在多种对象,每种对象都有自己的右键菜单,每种对象有一部分相同操作,刚接触js,麻烦写个类结构的例子

------解决方案--------------------
构造函数+原型。 自己去研究下。这是最简单的,没涉及到继承
HTML code

function Something(name,age){
    this.name=name;  //这里的属性,每个实例会复制一份,是自己的
    this.age=age;
}
Something.prototype={
    //这里的方法是共享的
    sayName:function(){
        alert(this.name)
    },
    sayAge:function(){
        alert(this.age);
    }
}
var s1=new Something('bill','30');
s1.sayName();
s1.sayAge();
var s2=new Something('jobs','40');
s2.sayName();
s2.sayAge();

------解决方案--------------------
function MAPS(property1, property2, property3, .......) {
this.property1 = property1;
this.property2 = property2;
this.property3 = property3;
..........................
this.method1 = function() {相同的操作1};
this.method2 = function() {相同的操作2};
this.method3 = function() {相同的操作3};
}

var MapObject = new MAPS(property1, property2, property3, .......);//生成实例
MapObject.method1; //调用方法

------解决方案--------------------
function object(){
this.config = {
attr1:1,
attr2:2
}
this.method = {
method1:function(){},
method2:function(){}
}
return {}
}