Q: is it possible to inherit from a native object?
Yes of course you can, but not necessarily in the way you mean here.
I tried to use Date object as the base object like so:
x.prototype = Date.prototype;
It is normal to 'inherit' the behaviour of an object by assigning an
instance of that object to the prototype of a constructor. Here you are
assigning a reference to another constructor's prototype. You will
inherit the methods and properties of the prototype in this way, but
that may not be sufficient in all cases.
x.prototype.constructor = x;
However, this is a dubious thing to do. By assigning a value to the
'constructor' property of the prototype of - x - you are also assigning
the value to the 'constructor' property of the prototype of - Date -. As
a result Date objects will look like - x - objects if you use their
'constructor' property to test for that.
function x(){
//constructor
}
var obj = new x();
ERROR ----> x.getDate();
This is what you have done. You have defined a function - x - as a
constructor and assigned the Date prototype to its 'prototype' property,
and then used - x - to construct a new object. That object will
'inherit' the methods defined on the Date prototype, but doing so is not
necessarily useful. The ECMA 262 definition of, for example, the getDate
method is:-
<quote cite="ECMA 262, 3rd edition; Section 15.9.5.14">
15.9.5.14 Date.prototype.getDate ( )
1. Let t be this time value.
2. If t is NaN, return NaN.
3. Return DateFromTime(LocalTime(t)).
</quote>
- and the first line in the algorithm for the method is 'Let t be this
time value', that is; the time value of the - this - object. If we look
at the definition of constructor for Date we find (for example):
<quote cite="ECMA 262, 3rd edition; Section 15.9.3.3">
15.9.3.3 new Date ( )
The [[Prototype]] property of the newly constructed object is set to
the original Date prototype object, the one that is the initial
value of Date.prototype (section 15.9.4.1).
The [[Class]] property of the newly constructed object is set to
"Date".
The [[Value]] property of the newly constructed object is set to the
current time (UTC).
</quote>
(all other variations of Date construction include these steps)
So the 'time value' of a Date object is the value assigned to its
internal [[Value]] property. The problem with inheriting the methods of
the Date object through its prototype is that your - x - constructors is
not capable of setting the internal [[Value]] property of the objects it
creates (ECMAScript has no mechanism for making such an assignment).
The - this - object constructed with - x - has no 'time value'. You can
inherit the method of Date through an object's prototype, but those
methods are useless to the constructed object, and will almost certainly
error if used.
Other native objects have method defined for their prototypes that may
explicitly be used with other objects. For example, nearly all String
object methods may be used by other objects. Where applicable the ECMA
262 includes a note along the lines of "The xxxxxxxx function is
intentionally generic; ..."
However, it is generally not that productive to attempt to inherit from
native objects through the prototype chain. Partly because not all of
the native objects methods are suitable for use with other types of
object but mostly because implementation bugs (particularly in IE) and
specification ambiguities (mostly relating to the interpretation of "If
this object does not have a property named by Result(n) ...") mean that
many methods that should be generic in theory are not in practice.
There are at least half a dozen other ways. Javascript is a very
flexible language, and because it has no classes (so all notions of
sub-classing are conceptual) approaches are better related to achieving
outcomes than following any single pre-defined pattern. For example, if
what you want is instances of a type of object that has all of the
behaviour of Date objects plus some additional features (effectively a
'sub-class' of Date) it is completely reasonable to have a 'factory'
function that passes arguments on to the Date constructor to create a
new Date instance and then augments that Date instance with additional
methods, etc. before returning it. E.G:-
function modifiedDateFactory(yearOrValue, month, date, h, m, s, ms){
var d = new Date (yearOrValue, month, date, h, m, s, ms);
// modify the new Date object here, by adding extra methods and
//properties or overloading existing public methods and properties.
return d;
}
(For information, when reading this group it is worth baring in mind
that VK's posts mostly consist of the fictional products of a deranged
mind. They are almost exclusively false, incomplete, misconceived or
represent the worst possible advice in any given situation)
Richard.