VK wrote:
Matt Kruse wrote:
List is an interface.
You cannot create a "List" object.
Think of "myArray extends Array implements List", that helps ;-)
How about:
Just enhance Array and call it List then
Another fully viable option.
----------
Array.prototype .add = function(elemen t,index) {
if (('number'!=typ eof index)||(index> =this.length)) {
this.push(eleme nt);
}
else {
this.splice(ind ex,0,element);
}
}
Array.prototype .remove = function(index) {
if (('number' != typeof index)||(index >= this.length)) {
return null;
}
else {
if (index<0) {index -= this.length;}
return this.splice(ind ex,1)[0];
}
}
var List = Array;
----------
Now, what value does it add? None, really. Under the hood, you're still just
using Array methods. So why not just call it an Array? You're not adding any
value by creating this "List" object.
That is a question of the kind "Why do you need to create class Manager
if it has only one property more than Employee? Simply add that
property to an Employee instance."
Or more generally: "Why do you need OOP hassle? Your custom objects do
not do anything what their supers couldn't, so just augment supers".
:-)
Having the prototype way as totally correct option, I need (think I
need, want) a separate object type, not just Array with extra methods.
Array is hadled as a fixed continuum defined by 0 - (length-1) borders.
We can extend this continuum or make "holes" ("gaps") in this continuum
by delete'ing elements.
List (interface) is flexible continuum able to automatically expand and
collapse ("healing the holes") on elements adding/removal.
That seems as enough of behavioral difference for a will to have two
different objects from different constuctors: rather than all existing
and future Array's acting this way or that way depending on what
methods do you apply to them.
Once again: prototype augmentation is definitely and always an option.
Since this looks like its getting out of control and Matt was the only
one kind enough to post code, I'm going to offer up the following as
well.
In keeping with the java.util.List inteface, I'm going to give the
size() method instead of length. I am also going to internalize the
methods of the List object. The constructor in this method will copy
the supplied array to prevent its modification (as in VK's posted
code).
I am also adding a few xUnit type tests to validate functionality and
to document usage.
I Hope it helps to clarify what others have been trying to explain.
<html>
<head>
<title>List in JS</title>
</head>
<script type="text/javascript">
function List(srcAry){
this._internAry = new Array();
if(srcAry){
/* Copy supplied array to prevent side effects */
this._internAry = new Array(srcAry.le ngth);
for(var i=0;i<srcAry.le ngth;i++){
this._internAry[i] = srcAry[i];
}
}
this.add = function(obj,in dx){
this._internAry .splice(indx,1, obj)
};
this.remove = function(indx){
return this._internAry .splice(indx,1) ;
};
this.toString = function(){
return this._internAry .toString();
}
this.size = function(){
return this._internAry .length;
}
}
</script>
<script type="text/javascript">
function assertEquals(ms g,val1,val2){
if(val1 != val2){
alert(msg + "\n expected: "+val1+"\nrecei ved: "+val2);
arguments.calle e.failed = +arguments.call ee.failed || 0;
arguments.calle e.failed++;
}
}
// Tests
var srcAry = new Array('Apples', 'Oranges','Bana nas');
assertEquals("S hould be 3 fruit",3,srcAry .length);
var myList = new List(srcAry);
assertEquals("t oString test
failed","Apples ,Oranges,Banana s",myList.toStr ing());
assertEquals("i ncorrect size on construction",3 ,myList.size()) ;
myList.add('Str awberries', 3);
assertEquals("S ize failed after adding",4,myLis t.size());
assertEquals("t oString test
failed","Apples ,Oranges,Banana s,Strawberries" ,myList.toStrin g());
assertEquals("S ource array modified (bad juju)",3,srcAry .length);
var fruit1 = myList.remove(1 );
assertEquals("w rong fruit","Oranges ",fruit1);
assertEquals("r esize failed after remove",3,myLis t.size());
assertEquals("t oString test
failed","Apples ,Bananas,Strawb erries",myList. toString());
assertEquals("S ource array modified (bad juju)",3,srcAry .length);
var fruit2 = myList.remove(1 );
assertEquals("w rong fruit","Bananas ",fruit2);
assertEquals("r esize failed after remove",2,myLis t.size());
assertEquals("t oString test
failed","Apples ,Strawberries", myList.toString ());
assertEquals("S ource array modified (bad juju)",3,srcAry .length);
if(assertEquals .failed){
alert("tests failed: " + assertEquals.fa iled);
}else{
alert("All tests passed");
}
</script>
<body>
</body>
</html>