Add-ons for Pale Moon and other applications
General discussion, compatibility, contributed extensions, themes, plugins, and more.
Moderators: FranklinDM, Lootyhoof
-
thosrtanner
- Lunatic
- Posts: 395
- Joined: 2014-05-10, 18:19
- Location: UK
Unread post
by thosrtanner » 2023-05-01, 10:20
I have some code in a module in an extension
Code: Select all
let some_date = new Date(some_string);
console.log(some_date, some_date instanceof Date);
Which outputs
Code: Select all
Date 2023-05-01T09:08:11.000Z true Module1.jsm:99
However if I then create an object using a method in another .jsm in the extension
Code: Select all
let some_date = new Date(some_string);
console.log(some_date, some_date instanceof Date);
thing = new Thing(some_date);
....
function Thing(some_date)
{
console.log(some_date, some_date instanceof Date);
this.myvar = some_date;
}
I get this:
Code: Select all
Date 2023-05-01T09:08:11.000Z true Module1.jsm:99
Date 2023-05-01T09:08:11.000Z false Module2.jsm:20
How come a date is not a date?
-
Moonchild
- Pale Moon guru
- Posts: 35651
- Joined: 2011-08-28, 17:27
- Location: Motala, SE
Unread post
by Moonchild » 2023-05-01, 10:48
thosrtanner wrote: ↑2023-05-01, 10:20
How come a date is not a date?
Passing it into a function will interpret it as a (date)String (passed by value) and not a dateObject (passed by reference).
You may want to use
new Date(some_date) inside the Thing() function to ensure it's an instance of the Date prototype (=making a copy).
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite
-
thosrtanner
- Lunatic
- Posts: 395
- Joined: 2014-05-10, 18:19
- Location: UK
Unread post
by thosrtanner » 2023-05-01, 11:02
It's not a string. It is most definitely a date object. if i click on the 'date' word in the 2nd line console log (it's highlighted blue), i can see all the date methods in the prototype. I can do date operations on the object in module2 (such as some_date.getFullYear()). It just isn't an instanceof Date.
-
Moonchild
- Pale Moon guru
- Posts: 35651
- Joined: 2011-08-28, 17:27
- Location: Motala, SE
Unread post
by Moonchild » 2023-05-01, 11:13
I'm not entirely sure in that case. Question is: is a different .jsm the same realm or a different one? If the realm is different then instanceof may not be accurate. Different realms means that they have different built-ins (different global object, different constructors, etc.). This may result in something unexpected when trying to check the prototype.
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite
-
thosrtanner
- Lunatic
- Posts: 395
- Joined: 2014-05-10, 18:19
- Location: UK
Unread post
by thosrtanner » 2023-05-01, 12:15
I don't think so, well, given that I have no idea what a realm is. They're both modules in the same extension. and there's only one window active when I'm doing this.
The workround of new Date(date) works but it's a bit fragile (in the sense it requires plenty of commenting so I don't think 'WTF' and remove it again in the future...)
-
Moonchild
- Pale Moon guru
- Posts: 35651
- Joined: 2011-08-28, 17:27
- Location: Motala, SE
Unread post
by Moonchild » 2023-05-01, 12:28
thosrtanner wrote: ↑2023-05-01, 12:15
The workround of new Date(date) works but it's a bit fragile
Well it's rather a limitation of the instanceof operator than the object. {jsm1}.Date.prototype simply won't be {jsm2}.Date.prototype as far as I understood. I'm just not too familiar with the extension/js module side of things.
thosrtanner wrote: ↑2023-05-01, 12:15
I have no idea what a realm is.
I'm pretty sure that .jsms act as their own realm, meaning their built-in prototypes are literally not the same, so instanceof won't be able to find the prototype in the object's chain (even if it's called the same and functionally the same) and fails.This should help for context:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof#instanceof_and_multiple_realms
Is it necessary to check the prototype?
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite
-
thosrtanner
- Lunatic
- Posts: 395
- Joined: 2014-05-10, 18:19
- Location: UK
Unread post
by thosrtanner » 2023-05-01, 12:34
I'll have a look. I want to serialise the object and don't want to serialise odd bits and pieces that javascript has added to my "class", just the data members. this is oldish code so there is probably a better way of getting hold of what I want than looping over all the attributes and discarding things that have a type of function or a type of object-but-not-Date