//JavaScript面向对象编程 var Class=function(){ } Class.extend=function(props){ //继承父类 var prototype=Object.create(this.prototype) //初始化函数ctor var _Class=function(){ if (this.ctor) this.ctor.apply(this, arguments); } //当前类属性和方法 for(var k in props){ prototype[k]= props[k] } _Class.prototype = prototype; //类继承 _Class.extend=this.extend; //类扩展 _Class.expand = function (prop) { for (var name in prop) { prototype[name] = prop[name]; } }; return _Class } //demo var Node=Class.extend({ name:"caoke1", ctor:function(name){ this.name=name } }) var Node2=Node.extend({ name:"caoke1", ctor:function(name){ this.name=name } }) cc=console var n=new Node("caoke2") var n2=new Node2("caoke3") cc.dir(n) cc.log(n2)
时间: 2024-10-10 14:15:23