javascript エラー「TypeError: Can only call Document.write on instances of Document」の解決方法
- 作成日 2022.07.19
- javascript
- javascript
![javascript エラー「TypeError: Can only call Document.write on instances of Document」の解決方法](https://mebee.info/wp-content/uploads/2022/02/javascript-1-890x500.png)
javascriptで、エラー「TypeError: Can only call Document.write on instances of Document」が発生した場合の原因と解決方法を記述してます。
環境
- OS macOS Big Sur
- ブラウザ safari 15.5
エラー内容
以下のコードを実行した際に発生。
let d = document.write;
d("hello");
エラーメッセージ
TypeError: Can only call Document.write on instances of Document
画像
![](https://mebee.info/wp-content/uploads/2022/07/image-39.png)
原因
ネイティブ関数の場合は、直接変数に代入しようとするとエラーとなります。
解決方法
一度、ラップしてから使用します。
let d = function(str) { document.write(str) };
d("hello");
実行結果
![](https://mebee.info/wp-content/uploads/2022/07/image-40.png)
-
前の記事
React.js styleを適用する 2022.07.19
-
次の記事
Linux ファイルから指定した行だけ抽出する 2022.07.19
コメントを書く