js 建立函數:js建立對象 對象如何繼承 及一些工具函數

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.