JS面对对象,继承父类给子类扩展属性

hi凹凸曼叔叔 2015-08-30 09:31:05


prototype.js中有下面三个扩展,根据这三个扩展可以模拟类,创建对象。

var Class = {
create: function(){
return function(){
this.initialize.apply(this,arguments);
};
}
};

var Extend = function(description, source){
for (var property in source){
description[property] = source[property];
}
return description;
};
Object.prototype.extend = function(obj){
return Extend.apply(this,[this,obj]);
};



//创建一个父类Person,子类Kid,子类Kid继承Person
var Person = Class.create();
Person.prototype = {
//构造函数
initialize:function(name,age){
this.name = name;
this.age = age;
},
//方法
say: function(){
//.....
}
}
var Kid = Class.create();
//继承person
Kid.prototype = (new Person()).extend({
//在这里可以给子类Kid扩展其他方法,也就是父类中没用的方法,比如下面的learn方法

learn:function(){
//.........
}
});





var anni = Kid("anna", 12);
anni.height = "1.5m";
anni.sex = "famale";
//此为实例对象后 给对象 扩展属性

我的问题是:不实例化对象 如何给子类扩展属性。。。
我想得到的办法就是在
exten({
})
里面重写构造函数,覆盖父类中的构造函数。。。

可能有的人还是不明白我是什么意思。往下看:
在java中

class Shape {
protected String name;
public Shape(String name) {
this.name = name;
}
}
class Circle extends Shape {
//Circle继承了Shape
//并扩展了自己的属性和方法
public double radius;
public Circle(double radius) {
this.radius = radius;
}
}

我想要的就是上面java中的这个效果 :子类继承父类的时候就可以扩展自己的属性

愿意交流的同学尽量贴代码。。。^-^
...全文
445 6 打赏 收藏 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
KK3K2005 2015-09-01
  • 打赏
  • 举报
回复
// ----------------------------------------------------------------------------- // Class object based on John Resigs code; inspired by base2 and Prototype // http://ejohn.org/blog/simple-javascript-inheritance/ var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\bparent\b/ : /.*/; var Class = function(){}; Class.extend = function(prop) { var parent = this.prototype; initializing = true; var prototype = new this(); initializing = false; for( var name in prop ) { if( typeof(prop[name]) == "function" && typeof(parent[name]) == "function" && fnTest.test(prop[name]) ) { prototype[name] = (function(name, fn){ return function() { var tmp = this.parent; this.parent = parent[name]; var ret = fn.apply(this, arguments); this.parent = tmp; return ret; }; })( name, prop[name] ); } else { prototype[name] = prop[name]; } } var ig = { copy: function( object ) { if( !object || typeof(object) != 'object' || object instanceof HTMLElement || //object instanceof ig.Class object instanceof Class ) { return object; } else if( object instanceof Array ) { var c = []; for( var i = 0, l = object.length; i < l; i++) { c[i] = ig.copy(object[i]); } return c; } else { var c = {}; for( var i in object ) { c[i] = ig.copy(object[i]); } return c; } } } function Class() { if( !initializing ) { // If this class has a staticInstantiate method, invoke it // and check if we got something back. If not, the normal // constructor (init) is called. if( this.staticInstantiate ) { var obj = this.staticInstantiate.apply(this, arguments); if( obj ) { return obj; } } for( var p in this ) { if( typeof(this[p]) == 'object' ) { this[p] = ig.copy(this[p]); // deep copy! } } if( this.init ) { this.init.apply(this, arguments); } } return this; } Class.prototype = prototype; Class.constructor = Class; Class.extend = arguments.callee; Class.inject = function(prop) { var proto = this.prototype; var parent = {}; for( var name in prop ) { if( typeof(prop[name]) == "function" && typeof(proto[name]) == "function" && fnTest.test(prop[name]) ) { parent[name] = proto[name]; // save original function proto[name] = (function(name, fn){ return function() { var tmp = this.parent; this.parent = parent[name]; var ret = fn.apply(this, arguments); this.parent = tmp; return ret; }; })( name, prop[name] ); } else { proto[name] = prop[name]; } } }; return Class; }; //早就有人写了这样的类 直接用呗 //例子 var BaseClass = Class.extend({ init:function(){ alert('base'); } }); var Myclass = BaseClass.extend({ name:null ,init:function(_name){ this.parent(); this.name= _name; alert('Myclass'); } ,Name:function(){ alert(this.name); } }); new Myclass('kk').Name();
  • 打赏
  • 举报
回复
js 根本不支持类型系统,它的 function 就会创建对象本身。你偏要单独开发一个类型系统,把明明是 function 中才需要的事情放到类型系统中去做,就累赘了。
  • 打赏
  • 举报
回复
我不知道纠结“不实例化”有什么现实的意义。给你写一个“经过实例化”而产生继承的例子: 对于类型 Error,如果需要扩展一个自定义的类,增加一个 email 新属性,那么可以在实例化函数中进行扩展
    function NotImplementsError(message, email) {
        this.message = Common.StringFormat("尚未实现此功能:{0}", message);
        this.email = email;
    }
    NotImplementsError.prototype = new Error();
原来的 Error 类只有 message 属性,这回增加了一个。
  • 打赏
  • 举报
回复
“不实例化对象”有现实什么意义吗?
qq_24603759 2015-08-30
  • 打赏
  • 举报
回复
你的思路有点乱。参考构造器原型混合继承和模块模式继承
似梦飞花 2015-08-30
  • 打赏
  • 举报
回复
js没用自带的继承 只能自己扩展到原型上 或者自己写个extend方法 但实现是一样的 或者直接扩展对象

87,984

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧