ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
| この記事の要点 |
|
このエラーが出る典型例
// (1) DOM API を短く書こうとして this を失う
const $ = document.querySelector;
$('div');
// Uncaught TypeError: Illegal invocation
// (2) console.log を別名に代入して呼ぶ
const log = console.log;
log('hello');
// Chrome/Firefox では動くが、古いブラウザでエラー
// (3) setTimeout に直接 method を渡す
setTimeout(document.querySelector, 100, 'div');
// → Illegal invocation
// (4) Array.forEach で this を渡し忘れる
[document.body].forEach(el => el.appendChild);
// 呼出時に Illegal invocation
原因: this の喪失
document.querySelector のような ネイティブメソッドは、内部で this が HTMLDocument のインスタンスであることを期待しています。別の変数に代入して呼ぶと、this が undefined(strict mode)や window(非 strict)になるため、ネイティブコードが「この呼び出し方は不正だ」と判定して投げるエラーが Illegal invocation です。
// 内部実装イメージ(疑似コード)
HTMLDocument.prototype.querySelector = function(selector) {
if (!(this instanceof HTMLDocument)) {
throw new TypeError('Illegal invocation');
}
// ...
};
// だから:
document.querySelector('div'); // this = document → OK
const fn = document.querySelector;
fn('div'); // this = undefined/window → エラー
対処1: bind で this を固定
// bind 第1引数に this を渡す
const $ = document.querySelector.bind(document);
$('div'); // OK
const $$ = document.querySelectorAll.bind(document);
$$('p'); // OK
// console.log も同じ
const log = console.log.bind(console);
log('hello'); // OK
対処2: アロー関数で包む
// アロー関数の中なら this が変わらない
const $ = (sel) => document.querySelector(sel);
$('div'); // OK
const $$ = (sel) => document.querySelectorAll(sel);
// 引数を受け流す ...rest
const log = (...args) => console.log(...args);
log('hello', 'world');
対処3: call / apply で呼ぶ
const fn = document.querySelector;
// call: 引数を個別に
fn.call(document, 'div');
// apply: 引数を配列で
fn.apply(document, ['div']);
対処4: setTimeout に渡すとき
// NG
setTimeout(document.querySelector, 100, 'div');
// OK: bind
setTimeout(document.querySelector.bind(document), 100, 'div');
// OK: アロー関数で包む(推奨)
setTimeout(() => document.querySelector('div'), 100);
jQuery で見るパターン
// jQuery でラップしたものをネイティブ呼出
const $div = $('#myDiv');
$div.click; // 関数オブジェクトだが、呼び出すと this 喪失する場合がある
// イベントハンドラ内で this を失う
$('#btn').on('click', function() {
setTimeout(this.appendChild, 0, somenode);
// → Illegal invocation (this が要素ではなく window に)
// 修正: bind か アロー
setTimeout(this.appendChild.bind(this), 0, somenode);
setTimeout(() => this.appendChild(somenode), 0);
});
FormData / fetch でも出る
// よくあるアンチパターン
const formData = new FormData();
const append = formData.append;
append('key', 'value');
// Illegal invocation
// 修正
const append = formData.append.bind(formData);
// または
formData.append('key', 'value');
エラーが出やすい API 一覧
| API | 必要な this |
|---|---|
document.querySelector / querySelectorAll | document |
document.createElement | document |
console.log / error / warn | console(一部ブラウザ) |
FormData.append | FormData インスタンス |
Headers.append / set / get | Headers インスタンス |
URL.searchParams.get | URLSearchParams |
localStorage.getItem | Storage |
HTMLElement.appendChild | HTMLElement |
| WebAudio API 全般 | 各 Node |
デバッグの手順
- スタックトレースで、エラー発生行のメソッド名を見つける
- そのメソッドが ネイティブの DOM/Web API か確認
- 呼び出し直前で
thisがどうなっているかdebuggerやconsole.log(this)で確認 - this が undefined / window / 別のオブジェクトなら、bind か アロー関数で修正
FAQ
Q: ES2022 以降では出なくなる?
A: 仕様自体は変わりません。this 喪失の問題は構造的なものなので、bind / アローで対処する以外ありません。
Q: TypeScript で防げないか
A: strictBindCallApply と noImplicitThis を有効にすると、代入時に this 不整合を型エラーで検出できる場合があります。
Q: console.log は今や普通に代入できる
A: モダンブラウザでは緩和されています。古い IE や一部の Edge では Illegal invocation が出るため、レガシー対応が必要なら bind 推奨。
ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
子ページ
子ページはありません
同階層のページ
- Uncaught TypeError: Illegal invocation
- Form submission canceled because the form is not connected
- Uncaught TypeError: location.href is not a function
- Access to XMLHttpRequest at 'url1' from origin 'url2' has been blocked
- Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')
人気ページ
- 1 Eclipseで「サーバーに追加または除去できるリソースがありません。」の原因と対処法
- 2 tomcat の起動 / 停止ログと catalina.log・catalina.out の違い
- 3 JavaScript base URL 取得方法|window.location.origin と SSR/Node.js 対応
- 4 YouTube Data API v3 エラー一覧|403/400/404 の主要原因と切り分け
- 5 Spring Frameworkのアノテーション一覧
- 6 Laravel エラー一覧|500/Blade/DB 接続/ルーティングの代表エラー
- 7 3Dグラフィックスとは|モデリング/レンダリング/主要ソフトウェア (Blender / Maya)
- 8 【Spring】@Valueアノテーションとは
- 9 CATALINA_HOME の確認方法 (Linux / Mac)
- 10 【Spring】@Autowiredアノテーションとは
最近更新/作成されたページ
- Laravel キャッシュクリア完全ガイド(cache:clear / config:clear / 2026-05-18 07:42:07
- プロジェクトの作成と削除 2026-05-18 07:42:07
- インストール直後にNetbeansが反応しない 2026-05-18 07:42:07
- 動画やチャンネルの検索 2026-05-18 07:42:07
- APIキー取得方法 2026-05-18 07:42:07
- チャンネル情報の取得 2026-05-18 07:42:07
- API 入門 — Web API(REST / GraphQL / gRPC / 2026-05-18 07:42:07
- インストール(eclipseプラグイン) 2026-05-18 07:42:07
- Laravel「Dotenv values containing spaces must be surrounded 2026-05-18 07:42:07
- エラー一覧 2026-05-18 07:42:07
- curl: (51) SSL: certificate subject name '~' does not match 2026-05-18 07:42:07
- インストール方法(Windows版) 2026-05-18 07:42:07
- JSONから配列に変換 2026-05-18 07:42:07
- 処理を一定時間待つ 2026-05-18 07:42:07
- A non well formed numeric value encountered 2026-05-18 07:42:07
コメントを削除してもよろしいでしょうか?