JS继承的DEMO

初学者的一个js继承的DEMO。

jQuery继承:

使用copy prototype的方式实现。

好处:

1.不会污染其它JS代码:因为一个普通的JS方法都是全局,因为被别的方法污染。此方法里所有function都内部实现,相当于一个java类及内部方法,写JAVA代码的同学是不是觉得很熟悉?

2.有点面向对象的味道,比如用户管理,我会定义一个userManager=new UserManager()的JS方法,全程处理用户管理所有的JS。同样可以定义Util啥啥的。如果加上包那么,基结构与java很类似了,前端使用比较少的人会对javascript更亲切了。

Here is a simply example as following:

//super,use: var manager=new Manager();

function Manager(config){

this.init(config || {});

}

Manager.prototype = {

tree:"Mytree",

init : function(config){

$.extend(this,config);

},

alert:function(){

alert(this.tree);

}

}

//child

function AManager(config) {

Manager.apply(this, arguments);

}

$.extend(AManager.prototype, Manager.prototype, {

init : function(config) {

Manager.prototype["init"].apply(this, arguments);

},

});

//test

$(document).ready(function() {

var a=new AManager({tree:"1"});

a.alert();//将显示1

});

posted on 2012-06-27 10:05  夏雨的天空  阅读(211)  评论(0编辑  收藏  举报

导航