So I'am wrong that inheritance in JavaScript should be written:
function Base () {}
function Derived () {
Base.call(this);
}
Derived.prototype = new Base;
Derived.prototype.constructor = Derived;
If so, how do I allow for natives to be inherited in v8?
Is this a v8 or JavaScript limitation, or it just can't be done?
On Mar 17, 4:06 pm, "Lasse R.H. Nielsen" <[email protected]> wrote:
> It won't work.
>
> In JavaScript, the prototype based inheritance is only property inheritance.
> It does not extend to internal fields, like [[Class]] or internal values of,
> e.g., Date, Number, String, or Boolean.
>
> You will have the exact same behavior if you try to inherit from, e.g.,
> Date.
> The internal time value of a Date object belongs to the Date object only, it
> is not inherited along the prototype chain.
> That means that an object created using:
> function Foo(){};
> Foo.prototype = new Date();
> var foo = new Foo;
> does not function as a Date object - it's internal [[Class]] property isn't
> "Date" and the Date specific functions will throw exceptions if called,
> e.g.:
> print(Object.prototype.toString.call(foo)); // prints [object Object],
> not [object Date].
> print(foo.getTime()); // throws "TypeError: this is not a Date object."
>
> My guess at the problem of test 3 is that you are using the Base
> constructor on an object that isn't created from the Base object
> template. That means that the object doesn't have an internal field slot 0
> (or if it has, it's being used for something else), and you are just reading
> and writing some other field of the object that is later overwritten again,
> possibly by the foo property. The same happens in test 2, but the field just
> happens not to be overwritten before it's read.
> (I.e., the code is unsafe, since it assumes an internal field count, but
> doesn't check that the object is created from the correct template).
>
> Best of luck.
> /L
>
> 2010/3/17 Henrik Lindqvist <[email protected]>
>
>
>
> > "IIRC, this isn't the way to do it in v8"
>
> > I don't want to force users to do it the V8 way, I want them to do it
> > the JavaScript way.
>
> > You write in your derived constructor:
> > function MyPanel() {
> > var argv = Array.prototype.slice.apply(arguments,[0]);
> > this.prototype = this.__proto__ = new ncurses.NCPanel(argv);
> > // ...
> > return this;
> > }
>
> > What is the property this.prototype?
> > In the constructor, isn't "this" the instance, then it has no
> > "prototype" property, that is on the constructor (function).
> > I don't see why I should use the "__proto__" property to change the
> > prototype chain after the instance was created, the prototype chain
> > has already been specified.
>
> > On Mar 17, 11:38 am, Stephan Beal <[email protected]> wrote:
> > > On Wed, Mar 17, 2010 at 2:44 AM, Henrik Lindqvist <
>
> > > [email protected]> wrote:
> > > > "Derived2.prototype = new Base; \n"
> > > > "Derived2.prototype.constructor = Derived2; \n"
>
> > > IIRC, this isn't the way to do it in v8. i remember going through similar
> > > pain when i wrote my ncurses wrappper for v8, and now i find that i
> > > documented it:
>
> > >http://code.google.com/p/v8-juice/wiki/PluginNCurses
>
> > > see the section called "Inheritance...", near the end of the table of
> > > contents.
>
> > > i hope that helps.
>
> > > --
> > > ----- stephan bealhttp://wanderinghorse.net/home/stephan/
>
> > --
> > v8-users mailing list
> > [email protected]
> >http://groups.google.com/group/v8-users
>
> --
> Lasse R.H. Nielsen
> [email protected]
> 'Faith without judgement merely degrades the spirit divine'
> Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 København K -
> Denmark - CVR nr. 28 86 69 84
--
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users