JavaScript inheritance, call ()

Source: Internet
Author: User

<! DOCTYPE html>

<meta charset= "UTF-8" >
<title></title>
<script type= "Text/javascript" >
(function () {
var Shape = new Object ();
Shape.color = "#O8c671";
Shape.area = "50px";
document.write ("Shape's color:" + Shape.color + "," + "shape's area is:" + Shape.area + "Parent object <br/>");

function Circle () {
document.write ("Circle color:" + This.color + "," + "Circle area is:" + This.area + "sub-object <br/>")
}
Circle.call (Shape);

function Rect () {
document.write ("Rect color:" + This.color + "," + "rect area is:" + This.area + "sub-object <br/>")
}
Rect.call (Shape);
})();
</script>

<body>
</body>
This case defines shape as the parent object, Circle and rect as child objects, and an inheritance relationship, and after inheriting two subclasses have the parent property color and area, line 19th calls the call method, the This reference is specified as shape, So the this.color in the function circle is equal to Shape.color. So we have the parent attribute, and the following rect is similar

Shape's color: #O8c671, Shape's area is: 50px Parent object
Color of Circle: #O8c671, the area of Circle is: 50px Sub-object
Color of rect: #O8c671, the area of rect is: 50px Sub-object
This case defines shape as the parent object, Circle and rect as child objects, and an inheritance relationship, and after inheriting two subclasses have the parent property color and area, line 19th calls the call method, the This reference is specified as shape, So the this.color in the function circle is equal to Shape.color. So we have the parent attribute, and the following rect is similar

JavaScript inheritance, call ()

Contact Us

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

  • 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.