object inheritance

4 views
Skip to first unread message

wayne

unread,
Jan 28, 2011, 10:49:13 AM1/28/11
to GLUEscript
I'm trying to create an object based on a GS library object that I can
give additional properties & reuse. Why doesn't the following work?
(Or just let me know the right way to do this)

var sqlite = require("sqlite");
function myDatab (fname)
{
sqlite.Database.call(this,fname)
this.myProp=true
}
myDatab.prototype = new sqlite.Database;
var db = new myDatab("person.db");


Franky Braem

unread,
Jan 28, 2011, 1:20:10 PM1/28/11
to glues...@googlegroups.com
This fails because sqlite.Database.call will execute the constructor and
the constructor of sqlite.Database expects that is called with the new
keyword. Most classes in GLUEscript work this way (wx.Point for example
does work without the new keyword). I'll try to look into this and make
it possible to create objects without using the new keyword.

Note that the code "new sqlite.Database" fails to create a database
because it needs an argument.

Franky

Op 28/01/2011 16:49, wayne schreef:


> I'm trying to create an object based on a GS library object that I can

> give additional properties& reuse. Why doesn't the following work?

Hans Schmucker

unread,
Jan 28, 2011, 1:23:39 PM1/28/11
to glues...@googlegroups.com
You can't always do that with JS :)

But the general right way to what you're tring to do would be this
(but it doesn't work here);

var inheritingContructor=function(arg0){
baseContructor.call(this,arg0);
};

(function(){
var tempCtor=function(){};
tempCtor.prototype=baseContructor.prototype;
inheritingContructor.prototype=new tempCtor();
})();

inheritingContructor.prototype.myProp=true;


On Fri, Jan 28, 2011 at 7:16 PM, bryan t <yamt...@gmail.com> wrote:
> Ok thanks, I'm new to JS & just want to make sure I'm doing it right
>
> On Fri, Jan 28, 2011 at 9:14 AM, <hanssc...@gmail.com> wrote:
>> That sadly rarely works with builtin objects...
>> the best way is probably mirroring the the DOM approach, using a create
>> function. It's not technically inheritance but it works.


>>
>> var sqlite = require("sqlite");
>>

>> var MyDatab=function(){
>> throw("use createMyDatab to initiate");
>> };
>>
>> MyDatab.prototype={
>> myProp:true
>> };
>>
>> var createMyDatab(fname){
>> var r=new sqlite.Database(fname);
>> for(var i in MyDatab.prototype)
>> r[i]=MyDatab.prototype[i];
>>
>> return r;
>> };

>>> --
>>>
>>> You received this message because you are subscribed to the Google Groups
>>> "GLUEscript" group.
>>>
>>> To post to this group, send email to glues...@googlegroups.com.
>>>
>>> To unsubscribe from this group, send email to
>>> gluescript+...@googlegroups.com.
>>>
>>> For more options, visit this group at
>>> http://groups.google.com/group/gluescript?hl=en.
>>>
>>>
>>>
>>>
>

--
Hans Schmucker
Mannheim
Germany
hanssc...@gmail.com
http://www.tapper-ware.net

Franky Braem

unread,
Jan 28, 2011, 1:23:58 PM1/28/11
to glues...@googlegroups.com
Some more info about using new or not:
http://stackoverflow.com/questions/1928342/using-constructor-without-operator-new

Franky

Op 28/01/2011 16:49, wayne schreef:

> I'm trying to create an object based on a GS library object that I can

> give additional properties& reuse. Why doesn't the following work?

wayne

unread,
Jan 28, 2011, 8:19:04 PM1/28/11
to GLUEscript
Ok , thanks for the clarification
Reply all
Reply to author
Forward
0 new messages