Thomas said:
From
http://developer.mozilla.org/en/doc...ting_New_Objects:Defining_Getters_and_Setters
(slightly adapted)
var d = Date.prototype;
d.__defineGetter__("year", function() { return this.getFullYear(); });
d.__defineSetter__("year", function(y) { this.setFullYear(y); });
var now = new Date();
window.alert(now.year); // 2007
now.year = 2001;
window.alert(now); // "current" date and time, but in 2001
WFM in Firefox 2.0.0.6.
You know what an assignment is.
I know that, but I could not make anything of your statement that it would
not be possible to overload _"assign"_ as you already mentioned "set", which
is the same thing.
Maybe you should elaborate on such general statements in the first place.
Etc. is such as compare, iterators, etc.
That is a recursive statement, so not of much use.
User-defined comparators are possible using Array objects since JavaScript
1.1 (NN 3.0/NES 2.0, 1996-08 CE), JScript 2.0 (IE/MSHTML 3.0, 1996),
ECMA-262 (Edition 1, 1997-06).
Instead of comparing two values, one would use an array with two elements,
and sort that. If the first element of the sorted array equals the first
element of the unsorted array, then the first element is smaller (or larger)
than the second according to the used definition of order.
An example, with apologies to Lewis Carroll:
function Animal(iDanger)
{
this.danger = iDanger || 0;
}
Animal.prototype = {
constructor: Animal,
comparator: function(a, b)
{
if (a.danger < b.danger)
{
return -1;
}
else if (a.danger > b.danger)
{
return 1;
}
else
{
return 0;
}
}
};
var
snark = new Animal(-2),
boojum = new Animal(42),
animals = [boojum, snark];
animals.sort(snark.comparator);
if (animals[0] === snark)
{
window.alert("Snarks less wicked than Boojums might be"
+ "\n\tBut the Snark *was* a Boojum, you see.");
}
The algorithm could be wrapped into a method that is provided the first and
the second value as arguments. It would then create the array consisting of
those two elements, look for a comparator method and call Array::sort()
passing the method reference if such a method exists, and so on. However, I
would like the possibility to overload the comparison operators directly
instead, of course.
Iterators can be defined in JavaScript since version 1.7 (Firefox 2):
http://developer.mozilla.org/en/docs/New_in_JavaScript_1.7#Iterators
However, it is not that hard to define and use them in earlier versions and
other implementations without that language feature.
Apparently you don't understand that iterating also over inherited
enumerable properties may be wanted. Nevertheless, it is quite easy
to ignore inherited properties when iterating.
for (var p in o)
{
if (o.hasOwnProperty(p))
{
// ...
}
}
and the like.
PointedEars