V
VK
A while ago I wrote a "Vector data type" script
using DOM interface to select.options.
That was a (beautiful) mind game rather than
a practical thing to use.
Here is another attempt open for criticism, this
time dead serious. I really need an effective
Vector emulator for a project (as much effective
as something not implemeted in language but
emulated by means of the language itself is: a
productivity impact is imminent, the question
is to minimize it).
<html>
<head>
<title>Vector constructor</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script>
function Vector(arr) {
this.$_$ = arr || new Array();
this.length = this.$_$.length;
this.add = Vector.$add;
this.remove = Vector.$remove;
this.toString = Vector.$toString;
}
Vector.$add = function(m,i) {
if (('number'!=typeof(i))||(i<0)||(i>=this.$_$.length)) {
this.$_$.push(m);
}
else {
var tmp = [m];
tmp.push(this.$_$);
this.$_$.splice(i,1,tmp);
}
this.length = this.$_$.length;
}
Vector.$remove = function(i) {
var ret = this.$_$.splice(i,1)[0];
this.length = this.$_$.length;
return ret;
}
Vector.$toString = function() {
return this.$_$.toString();
}
// Create new vector and use it as a wrapper
// over Array argument:
var v = new Vector([1,2,3]);
// add(newElement, atPosition) method
// if no atPosition provided, newElement will
// be added to the top
v.add(4); // add 4 to the top
// Add 3.5 atPosition 3
// The element currently located at v[3] and all elements atop
// of it will be moved one position up
v.add(3.5, 3);
// remove(atPosition) method
// Elements currently located atop of it will be moved
// one position down
v.remove(1);
// toString method is overloaded so in string
// context it gives comma-separated vector values
alert(v); //1, 3, 3.5, 4
</script>
</head>
<body>
</body>
</html>
using DOM interface to select.options.
That was a (beautiful) mind game rather than
a practical thing to use.
Here is another attempt open for criticism, this
time dead serious. I really need an effective
Vector emulator for a project (as much effective
as something not implemeted in language but
emulated by means of the language itself is: a
productivity impact is imminent, the question
is to minimize it).
<html>
<head>
<title>Vector constructor</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script>
function Vector(arr) {
this.$_$ = arr || new Array();
this.length = this.$_$.length;
this.add = Vector.$add;
this.remove = Vector.$remove;
this.toString = Vector.$toString;
}
Vector.$add = function(m,i) {
if (('number'!=typeof(i))||(i<0)||(i>=this.$_$.length)) {
this.$_$.push(m);
}
else {
var tmp = [m];
tmp.push(this.$_$);
this.$_$.splice(i,1,tmp);
}
this.length = this.$_$.length;
}
Vector.$remove = function(i) {
var ret = this.$_$.splice(i,1)[0];
this.length = this.$_$.length;
return ret;
}
Vector.$toString = function() {
return this.$_$.toString();
}
// Create new vector and use it as a wrapper
// over Array argument:
var v = new Vector([1,2,3]);
// add(newElement, atPosition) method
// if no atPosition provided, newElement will
// be added to the top
v.add(4); // add 4 to the top
// Add 3.5 atPosition 3
// The element currently located at v[3] and all elements atop
// of it will be moved one position up
v.add(3.5, 3);
// remove(atPosition) method
// Elements currently located atop of it will be moved
// one position down
v.remove(1);
// toString method is overloaded so in string
// context it gives comma-separated vector values
alert(v); //1, 3, 3.5, 4
</script>
</head>
<body>
</body>
</html>