ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子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
- 4 YouTube Data API v3 エラー一覧|403・400・404 の原因と対処
- 5 Laravel エラー一覧|500/Blade/DB 接続/ルーティングの代表エラー
- 6 3Dグラフィックスとは|モデリング/レンダリング/主要ソフトウェア (Blender / Maya)
- 7 Spring Frameworkのアノテーション一覧
- 8 【Spring】@Valueアノテーションとは
- 9 CATALINA_HOME の確認方法 (Linux / Mac)
- 10 【Spring】@Autowiredアノテーションとは
最近更新/作成されたページ
- 【django】ログイン 認証機能 2026-09-08 03:22:09
- MySQL ERROR 1063 Incorrect column specifier for column|原因と直し方 2026-09-08 03:11:09
- 【PHPエラー】Object of class stdClass could not be converted to string 2026-09-07 07:46:04
- Julia Genie ローカル開発サーバ起動完全ガイド 2026-09-07 07:46:04
- Wi-Fi とは|規格と世代(Wi-Fi 4〜7)・周波数帯・CSMA/CA・WPA3 NEW 2026-09-07 07:45:13
- set コマンドでシェルオプションと位置パラメータを操作 | bash 2026-09-07 07:45:13
- JPEG(.jpg/.jpeg)画像形式の完全ガイド — 仕様・マジックナンバー・他形式との比較・EXIF 2026-09-07 07:45:13
- UE5 Get Overlapping Actorsで特定クラスだけ処理|Class Filter・Cast To 2026-09-07 07:45:13
- Linuxで特定拡張子のファイルを再帰削除|find -deleteの安全手順 2026-09-07 07:45:13
- Django CBVでテンプレートに値を渡す|get_context_data・extra_context 2026-09-07 07:45:13
- Linuxで複数ファイルの文字列を一括置換|find・sed・xargsの安全手順 2026-09-07 07:45:13
- Laravelルートグループ|prefix・middleware・nameで一括管理 2026-09-07 07:45:13
- Linux whichコマンドの使い方|絶対パス取得とtype・command -vの違い 2026-09-07 07:45:13
- Linux catコマンドの使い方|行番号表示・複数ファイル連結 2026-09-07 07:45:13
- unable to execute 'gcc': No such file or directory 2026-09-07 07:45:13
コメントを削除してもよろしいでしょうか?