function Rectangle(w, h) {
this.width = w;
this.height = h;
}
Rectangle.prototype.area = function () { return this.width * this.height; }; //类的实例方法 建议为原型对象定义方法而不是放在构造函数定义方法
Rectangle.prototype.toString = function () { return "[" + this.width + "," + this.height + "]"; };//这样性能更好,所有的对象共享同一个方法,对象继承之对象原型
function PositionRectangle(x, y, w, h) {
// Rectangle.apply(this, [w, h]); //调用父类的构造函数
this.superclass(w, h); //通过给原型追加属性 调用父类的构造函数
this.x = x;
this.y = y;
}
PositionRectangle.prototype = new Rectangle();//子类的原型为超类的实例
PositionRectangle.prototype.superclass = Rectangle;//为原型添加属性方便调用父类构造函数
PositionRectangle.prototype.constructor = PositionRectangle; //设置原型的constructor
delete PositionRectangle.prototype.width;//删除超类构造函数在原型中创建的属性
delete PositionRectangle.prototype.height; //删除超类构造函数在原型中创建的属性
PositionRectangle.prototype.contains = function (x, y) {
return (this.x < x && x < this.x + this.width, this.y < y && y < this.y + this.height);
}
PositionRectangle.prototype.toString = function () {
return "(" + this.x + "," + this.y + ")" + this.superclass.prototype.toString.apply(this);//调用父类的同名方法
};
function getPropertyName(/*object */o, /*option array*/r) {
var r = r [];
for (var name in o) {
r.push(name);
}
return r;
}
function copyProperties(/*object from*/from, /*object to*/to) {
var to = to {};
for (var p in from) {
to[p] = from[p];
}
return to;
}
function copyUndefinedProperties(/*object from*/from, /*object to*/to) {
for (var p in from) {
if (to[p] === undefined)
to[p] = from[p];
}
return to;
}
function filterArray(/*Array*/a, /*predicate*/predicate) { 本文链接http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20121116/33888.html
欢迎大家阅读《js 创建函数:js创建对象 对象如何继承 …_js》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码