JS Create function: JS Create object object How to inherit and some tool functions
Source: Internet
Author: User
function Rectangle (W, h) {
This.width = W;
This.height = h;
}
Rectangle.prototype.area = function () {return this.width * this.height;}; The instance method of a class suggests defining a method for a prototype object rather than a constructor definition method
Rectangle.prototype.toString = function () {return "[+ This.width +", "+ This.height +]";};/ /This performance is better, all objects share the same method, object inherits the object prototype
function Positionrectangle (x, Y, W, h) {
Rectangle.apply (This, [w, H]); Calling the constructor of the parent class
This.superclass (W, h); Call the constructor of the parent class by appending the property to the stereotype
this.x = x;
This.y = y;
}
Positionrectangle.prototype = The prototype of the new Rectangle ()///subclass is an instance of the superclass
PositionRectangle.prototype.superclass = rectangle;//Add attributes to the prototype to facilitate the invocation of the parent class constructor
PositionRectangle.prototype.constructor = Positionrectangle; Set the constructor of the prototype
Delete positionrectangle.prototype.width;//deletes the properties created by the superclass constructor in the prototype
Delete PositionRectangle.prototype.height; Remove a property created by a superclass constructor in a prototype
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); Call method with the same name as the parent class
};
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. from) {
TO[P] = from[p];
}
return to;
}
function copyundefinedproperties (/*object from*/from,/*object to*/to) {
For (Var p. from) {
if (to[p] = = undefined)
TO[P] = from[p];
}
return to;
}
function Filterarray (/*array*/a,/*predicate*/predicate) {This article links http://www.cxybl.com/html/wyzz/JavaScript_Ajax/ 20121116/33888.html
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service