首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

各位 帮忙设计一下类结构吧,该如何处理

2012-04-17 
各位 帮忙设计一下类结构吧地图上存在多种对象,每种对象都有自己的右键菜单,每种对象有一部分相同操作,刚

各位 帮忙设计一下类结构吧
地图上存在多种对象,每种对象都有自己的右键菜单,每种对象有一部分相同操作,刚接触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 {}
}

热点排行