It is my understanding that the "standard" way to define a new ClassB extending ClassA is as following:
function ClassA() {
this.a = {}; // ClassA's instance member.
}
ClassA.prototype.meth1 = function () { .. } // ClassA's method, shared by all instance.
function ClassB() {}
ClassB.prototype = new ClassA() // <== "standard" way to extend
ClassB.prototype.meth2 = function () {...} // ClassB's method
When I try to define a new class ArrayX like following:
function ArrayX() {
}
ArrayX.prototype = new Array()
ArrayX.prototype.removeDup = function removeDup() {
var o = [];
for(var j=0; j<this.length; j++) {
if(notExist(this[j])
o.push(this[j])
}
return o
function notExist(itm) {
for(var j=0; j<o.length; j++) {
if(o[j]===itm)return false
}
return true;
}
var x = new ArrayX();
console.log(x.length) // returns 0. Good
console.log(x) // returns []. Good
x[0] = 0;
console.log(x); // returns []. No good. I expect x should have one element.
(new ArrayX([1,1,2,3,3])).removeDup() // I expect returns [1,2,3]
I know I can define the function-removeDup in following way:
Array.prototype.removeDup = function removeDup() { ...}
However, I just want to define a new class extending some standard javascript class like Array, or, even the DOM classes.
So, how to define a new class extending standard javascript class like Array?