TypeError: XXX is not a constructor when I use a library

368 views
Skip to first unread message

Jorge Forero

unread,
May 2, 2021, 7:09:11 PM5/2/21
to Google Apps Script Community
Hi guys,

I'm developing a library that extends the Date Object:

// FILE Prop.gs
/**
* constructor
*/
class EadDate extends Date {
constructor() {
super();
this.msperday = 1000 * 60 * 60 * 24;
}
};

/**
* date copy
*/
EadDate.prototype.copy = function () {
return new Date( this.getTime() );
}

/**
* getDaysBetween
*/
EadDate.prototype.getDaysBetween = function( EadDateEnd ) {
cloneDate = EadDateEnd.copy();
cloneDate.setHours( this.getHours(), this.getMinutes(), this.getSeconds(), this.getMilliseconds() );
let diff = cloneDate.getTime() - this.getTime();
return ( diff ) / this.msperday;
}

When I do test any function 'locally', It works fine!  
let birthday = new EadDate( '06/04/2021' );
let daysLeft = eadate.getDaysBetween( birthday );
console.log( `daysLeft= ${daysLeft}` );

Console:
Info
daysLeft= 32

Then, I do link the library's actual version to another gs file and I do test again:

// FILE test.gs with the library linked

/**
* TEST_Ead
*/
function TEST_Ead() {

let eadate = new Prop.EadDate( '05/03/2021' ); << ERROR IN THIS LINE
let birthday = new Prop.EadDate( '06/04/2021' );
console.log( `days= ${eadate.getDaysBetween( birthday )}` );
};

I get the following error:
Error
TypeError: Prop.EadDate is not a constructor

I'm using the new editor.  Any advice or comment about what's going on?

Thanks in advance for any help.

Jorge.


Tanaike

unread,
May 3, 2021, 4:18:33 AM5/3/21
to Google Apps Script Community
Unfortunately, I cannot understand about your script of "When I do test any function 'locally', It works fine!" using "// FILE Prop.gs". I cannot understand about the logic for achieving "daysLeft= 32" from the script. Can I ask you about the detail information for replicating your situation?

Adam Morris

unread,
May 3, 2021, 6:52:27 AM5/3/21
to google-apps-sc...@googlegroups.com
Nice idea! Great to see another attempt at libraries.

There is a vast wall between the host project and the libraries it links to. The wall is to such an extent that objects are isolated from each other. Which means that augmenting objects in a library will have no effect on the host project. This is probably a very good thing. 

However, you can attempt to send your date object to the library. That library can then augment as needed. The idea is that you have to send it to the library explicitly.

Here is an example:

// project code:
function myFunction() {
Lib.augment(Date); // very magical!
const date = new Date();
const now = Date.now(); // static method
const result = date.doSomething(); // instance method
Logger.log(result);
Logger.log(now);
}


// lib code:
function augment(obj) {
// add properties to the prototype, i.e. "instance methods"
obj.prototype.doSomething = () => {
return 1;
};
// add properties to the class, i.e. "static methods"
obj.now = () => new Date();
}



--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/CABPUPFHsroZifi2S-%3D8FVOThVcgZu0J1GDgcJpQJ-m86-Z%2BfLw%40mail.gmail.com.

Adam Morris

unread,
May 3, 2021, 7:29:07 AM5/3/21
to google-apps-sc...@googlegroups.com
I really had fun with this question, thanks.

Hopefully my previous post enlightened you. I wanted to figure this out, so I got it working according to what you displayed.

project code:

function myFunction2() {
const EadDate = Lib.augment2(Date); // very magical!
let eadate = new EadDate( '05/03/2021' );
let birthday = new EadDate( '06/04/2021' );
console.log( `days= ${eadate.getDaysBetween( birthday )}` );
}


// lib code:
function augment2(Obj) {
// add properties to the prototype, i.e. "instance methods"
return class EadDate extends Obj {
constructor (value) {
super(value);
this.msperday = 1000 * 60 * 60 * 24;
}

getDaysBetween (endDate) {
let diff = endDate.getTime() - this.getTime();
return diff / this.msperday;
}
}
}

Jorge Forero

unread,
May 3, 2021, 11:03:19 AM5/3/21
to Google Apps Script Community

Thanks to take the time on this matter.

@Tanaike
I apologize for my lack of clarity.  I want to have a Library file where I extend the date object.  To test the library methods, I write a temporary test function ( in the same library file ) calling all the methods to verify it's free of errors.  This is what I called "test locally".   One of the methods intends to calculate the number of days left between two dates.

@Adam Morris
Thanks for your help. From your explanation, I understood this issue better, so I applied your solution and that worked!

Greetings,

Tanaike

unread,
May 3, 2021, 8:48:32 PM5/3/21
to Google Apps Script Community
Thank you for replying. About my comment, I had thought that your script might be miscopied from your actual script. Because your script cannot replicate your sample result.

Reply all
Reply to author
Forward
0 new messages