Can't do Object.create for class inheritance

11 views
Skip to first unread message

dave mobley

unread,
Aug 30, 2019, 10:14:12 PM8/30/19
to JSDB
I was trying to do an Object.create on the super class to inherit the prototype functions, but I keep getting the error TypeError: Object.create is not a function. The documentation for SpiderMonkey 1.5 and ES5 both say this should work.

Anything obvious I might be missing?  I can provide a short code snippet if necessary.

S Rao

unread,
Aug 31, 2019, 12:12:42 AM8/31/19
to js...@googlegroups.com
Huh. I have never tried doing that. 

Shanti

On Aug 30, 2019, at 7:14 PM, dave mobley <scp...@gmail.com> wrote:

I was trying to do an Object.create on the super class to inherit the prototype functions, but I keep getting the error TypeError: Object.create is not a function. The documentation for SpiderMonkey 1.5 and ES5 both say this should work.

Anything obvious I might be missing?  I can provide a short code snippet if necessary.

--
You received this message because you are subscribed to the Google Groups "JSDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsdb+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jsdb/b4597c2d-68b7-4c69-8d94-367a3107376d%40googlegroups.com.

dave mobley

unread,
Sep 5, 2019, 7:48:15 PM9/5/19
to JSDB
So I've been trying to work around this and still no good ideas.   Here is an example:

var P = function()
{
    writeln
("running P constructor");
}

P
.prototype.doSomething = function(cool)
{
    writeln
(cool);
}

var C = function()
{
    writeln
("I'm the C constructor");
}

C
.prototype = new P(); // P's constructor will run here
C
.prototype.constructor = C;  // reset C's constructor back to the proper function

writeln
("If this were another language,this would be the first output.");
a_C_instance
= new C();
a_C_instance
.doSomething("Execute inherited function!");

By having to make a new instance of P to inherit to C, it causes P's constructor be invoked.  Object.create would have done this without invoking the constructor, but at least for me when I test with JSDB, there is no Object.create.  As far as I can tell, 'new' causes an instance of an object to be created from the prototypes and invocation of the constructor.

Any ideas on how I can just create a copy of the prototypes without invoking the constructor, in this case do something like 'Object.create(P)' instead of 'new P()'.

I hope that was clear, and any help would be appreciated.  Also trying not to move everything out of the constructor and into another function (like 'init()') because it would make the code messy.

Tahnks

dave mobley

unread,
Sep 5, 2019, 9:08:35 PM9/5/19
to JSDB
OK..I think I may have answered my own question, and here is how to do it.  Effectively, you need to copy all the functions and 'stuff' from the parent to the child.  I wrote a little function as part of Object to do this:

Object.inherit = function(parent, child)
{
 
for(key in parent.prototype)
 
{
 child
.prototype[key] = parent.prototype[key];
 
}
 
 
for(key in parent)
 
{
 child
[key] = parent[key];
 
}
 
 child
.prototype.constructor = child;
}


I just call 'Object.inherit(parent, child)' and it seems to work pretty well so far and inherits the way you'd expect (like Java or Python).
Reply all
Reply to author
Forward
0 new messages