6

I have a class Foo with a static method that compares this to Foo, and for some reason the output of that comparison is false:

// ==UserScript==
// @name     GreaseMonkey test
// @version  1
// @grant    none
// @include  *
// ==/UserScript==

window['Cls'] = class {};
window['func'] = function() {};

console.log(Cls === Cls);  // output: false
console.log(func === func);  // output: false

How can this be? I suspect it's related to the fact that Greasemonkey executes userscripts in a sandbox with elevated privileges, but even then I cannot understand why this would output false. Furthermore, the output changes to true if the function and the class aren't assigned to window:

class Cls {};
function func() {};

console.log(Cls === Cls);  // output: true
console.log(func === func);  // output: true

What is going on here?

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
  • This is really weird, wow. Can you also a) try with a `{}` object instead of a function b) try to evaluate the comparison multiple times (may it only returns something different on first access) c) log the values before comparing them? – Bergi Oct 14 '18 at 19:00
  • 1
    GM 4+ is all kinds of fubar. That's why I no longer support it. Just tell your users to use Tampermonkey or Violentmonkey, as [GM itself recommends](https://www.greasespot.net/2017/09/greasemonkey-4-for-users.html). – Brock Adams Oct 14 '18 at 19:02
  • 1
    Objects seem to behave as expected, they `===` each other and `===` the same thing on `window` and `unsafeWindow` – CertainPerformance Oct 14 '18 at 19:02
  • @BrockAdams I'm using GM 3.11, and the problem occurs there as well. – CertainPerformance Oct 14 '18 at 19:03
  • @Bergi a) `{}` objects compare equal to themselves. b) The result is `false` every time. c) logging the values yields the source code of the class/function, nothing unexpected. But I have discovered that the comparison returns `true` if I assign the class/function to a variable first: `var f = func; f === f // true` – Aran-Fey Oct 14 '18 at 19:13

0 Answers0