No, you are correct, this is how prototype base property inheritance works in JavaScript. However, prototype based inheritance is not the same as class based inheritance, and it doesn't work the way inheritance works in, e.g., Java.
This is a JavaScript limitation. Inheritance in JavaScript is *property* inheritance only. In class based inheritance, the inheriting class copies all structure from the base class, wheras in prototype based inheritance the inheriting object does not inherit any structure. It merely has a pointer to another object, and property lookups (only!) follow this pointer. In all other ways, the inheriting object is completely unrelated to the prototype. Another way to say the same is that there are no classes in JavaScript (in the sense of class based OO). Using constructors and prototypes can give something that emulates classes, but it is not the same. You can have your functions on the Base prototype follow the prototype chain of its "this" value to find an object that does implement the internal structure it needs (if any exist). However, that object will be shared between all instances of Derived, so the internal state will essentially be "static" (in class-based terms). You might be able to use __proto__ to create a new Base object that inherits Derived.prototype. E.g.: function Derived(args) { var base = new Base(); base.__proto__ = Derived.prototype; return base; } Derived.prototype = {__proto__: Base.prototype, constructor: Derived }; // [1] This ensures that new Derived() instanceof Derived new Derived() instanceof Base Object.toString.call(new Derived) == "[object Base]" and new Derived() has the necessary internal structure to support the functions inherited from Base.prototype. I.e., you need an object that is created using the ObjectTemplate you created for Base for the associated functions to work, so you must create one of those - which you can only do (from JavaScript) using "new Base". I think it should work (although I haven't actually checked yet). /L [1] I prefer this to using Derived.prototype = new Base(). If emulating class based inheritance, one should inherit from the class, not from an instance, and the prototype represents the class (more than the constructor function does). On Wed, Mar 17, 2010 at 17:12, Henrik Lindqvist <henrik.lindqv...@gmail.com>wrote: > 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" <l...@chromium.org> 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 <henrik.lindqv...@gmail.com> > > > > > > > > > "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 <sgb...@googlemail.com> wrote: > > > > On Wed, Mar 17, 2010 at 2:44 AM, Henrik Lindqvist < > > > > > > henrik.lindqv...@gmail.com> 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 > > > v8-users@googlegroups.com > > >http://groups.google.com/group/v8-users > > > > -- > > Lasse R.H. Nielsen > > l...@google.com > > '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 > v8-users@googlegroups.com > http://groups.google.com/group/v8-users > -- Lasse R.H. Nielsen l...@chromium.org '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 v8-users@googlegroups.com http://groups.google.com/group/v8-users