Subclasses and Superclasses
Subclasses and Superclasses
In Java and C++, there is an explicit concept of the class hierarchy. i.e. Every class can have a super class from which it inherits properties and methods. Any class can be extended, or sub-classed so the resulting subclass can inherit its parent's behavior. As we have seen, JavaScript supports prototype inheritance instead of class based. It's possible for inheritance to happen other ways, however.The following is an example of inheritance through functions.
<script language="javascript" type="text/javascript">
<!--
// thanks to webreference
function superClass() {
this.supertest = superTest; //attach method superTest
}
function subClass() {
this.inheritFrom = superClass;
this.inheritFrom();
this.subtest = subTest; //attach method subTest
}
function superTest() {
return "superTest";
}
function subTest() {
return "subTest";
}
var newClass = new subClass();
alert(newClass.subtest()); // yields "subTest"
alert(newClass.supertest()); // yields "superTest"
//-->
</script>
Prototype based inheritance is a far better bet for JavaScript
applications in most cases.reference : http://www.javascriptkit.com/javatutors/oopjs3.shtml
Comments
Post a Comment
silahkan berkomentar, kritik dan saran yang membangun adalah harapkan kita semua !