Use inheritance in JavaScript object-oriented programming (1)
A few days ago
JScript version of CollecionBase class
Used to resolve the base class that needs to be used as a class that is the primary data structure. However, there were no examples of inheritance at the time, and some netizens were more confused for JavaScript inheritance, so I used four ways to achieve four ArrayList derived classes today. About using JavaScript for object-oriented programming (OOP), there are many articles on the Internet say. Here I recommend two articles to see, if you don't understand how to use the Prototype property of JavaScript's Function object to implement class definitions and their principles, then take a closer look.
Object-oriented JavaScript programming
','
Object-oriented JScript
'with'
Classical Inheritance in JavaScript
'Oh (especially the first article and the article related to discussions), otherwise, behind the mist can't blame me. That CollectionBase is not listed. The first link is it. Here is the JavaScript 'inheritance' implemented in four ways (not quoted in the future. Construction inheritance method:
Function ArrayList01 () {this.base = CollectionBase; this.base (); this.m_array = this.m_innerarray; this.foo = function () {document.write (this ':' this.m_count ':' This.m_Array '
');}; this.add = function (item) {this.insertat (item, this.m_count);}; this.insertat = function (item, index) {this. M_innerarray.splice (index, 0, item); this.m_count ;}; this.tostring = function () {return '[class arraylist01]';};
script>
Optical inheritance method:
Function ArrayList02 () {this.insertat = function (item, index) {this.m_innerarray.splice (index, 0, item); this.m_count ;}; this.m_array = this.m_innerarray; this.tostring = function () {return '[class ArrayList02]';};} ArrayList02.prototype = new CollectionBase (); ArrayList02.prototype.foo = function () {document.write (this ':' this.m_Count ':' this .m_Array '
');}; arraylist02.prototype.insertat = function (item, index) {this.m_innerarray.splice (index, 0, item); this.m_count ;}; script>
Instance inheritance method:
Function arraylist03 () {var base = new collectionbase (); base.m_array = base.m_innerarray; base.foo = function () {document.write (this ':' this.m_count ':' this.m_Array '
');}; base.insertat = function (item, index) {this.m_innerarray.splice (index, 0, item); this.m_count ;}; base.tostring = function () {Return '[class arraylist03]';}; return base;}
script>
Additional inheritance method:
Function arraylist04 () {this.base = new collectionBase (); for (var key in this.base) {if (! this [key]) {this [key] = this.base [key];}} this.m_Array = This.m_innerarray; this.insertat = function (item, index) {this.m_innerarray.splice (index, 0, item); this.m_count ;};} arraylist04.prototype.foo = function () {document.write ( THIS ':' this.m_count ':' this.m_array '
');} arraylist04.prototype.tostring = function () {return '[class arraylist04]';} script>
Foo in the derived class is a newly added function that outputs the type of class and the data in m_innerarray. Tostring () is equivalent to ooverride's TString (), but it is true to assign a value, and '
Rename from the JavaScript function to its initialization
'The principle that is said in one article is the same. The principle and difference between this four methods I
Will be analyzed later
,wanted...