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?
{}
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? – Lillylillywhite===
each other and===
the same thing onwindow
andunsafeWindow
– Canzone{}
objects compare equal to themselves. b) The result isfalse
every time. c) logging the values yields the source code of the class/function, nothing unexpected. But I have discovered that the comparison returnstrue
if I assign the class/function to a variable first:var f = func; f === f // true
– Batchelor