20250202
code:javascript
(() => {
const Wrapped = {
confirm: (content) => window.confirm(content),
prompt: (content) => window.prompt(content),
};
const Referenced = {
confirm: window.confirm,
prompt: window.prompt,
};
Wrapped.confirm("are you sure?");
Referenced.confirm("aren't you sure?"); // Uncaught TypeError: 'confirm' called on an object that does not implement interface Window.
})();
Firefoxの開発者ツール上のREPLで上記のスクリプトを稼働させると、Referenced.confirmがエラーになる
追ってthis, globalThisでも検証したけどいずれもダメ
.bind()を使って元のオブジェクトにバインドしないといけない
code:javascript
(() => {
const ThisReferenced = {
confirm: this.confirm,
prompt: this.prompt,
};
const GlobalthisReferenced = {
confirm: globalThis.confirm,
prompt: globalThis.prompt,
};
ThisReferenced.confirm("aren't you sure?"); // Uncaught TypeError: 'confirm' called on an object that does not implement interface Window.
GlobalthisReferenced.confirm("aren't you sure?"); // Uncaught TypeError: 'confirm' called on an object that does not implement interface Window.
})();