您好,欢迎来到希系情感。
搜索
您的当前位置:首页javascriptBase类包含基本的方法_js面向对象

javascriptBase类包含基本的方法_js面向对象

来源:希系情感


代码如下:

function Base(){} //根抽象类
Base.toBase=function(){ //将一个对象转化成Base类的实例的方法
return new Base();
}
Base.inherit=function(parent){ //用于继承Base类的实例的方法
var F=function(){}
F.prototype=parent;
return new F;
}
Base.prototype.extend = function(prop){ //扩展根抽象类Base的extend方法
for (var o in prop) {
this[o] = prop[o];
}
}
Base.prototype.method = function(name, fn){ //扩展根抽象类Base的method方法
this[name] = fn;
return this;
}
var o=new Base(); //创建一个Base实例
o.method("show",function(){ //给对象o添加show方法
alert("show function");
});
o.extend({ //在给对象o添加name属性和say函数
name:"shupersha",
say:function(){
alert("say function")
}
});
var t=Base.inherit(o); //继承o对象的属性和方法
t.show();
t.say();

Copyright © 2019- xixiwg.cn 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务