pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Errors/Unexpected_type

image/svg+xml" href="https://developer.mozilla.org/favicon.svg" />

このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

TypeError: "x" is (not) "y"

JavaScript の例外 "x is (not) y" は、予期しない型があったときに発生します。よくあるのは、予期せず undefined または null の値があった場合です。

エラーメッセージ

js
TypeError: Unable to get property {x} of undefined or null reference (Edge)
TypeError: "x" is (not) "y" (Firefox)

例:
TypeError: "x" is undefined
TypeError: "x" is null
TypeError: "undefined" is not an object
TypeError: "x" is not an object or null
TypeError: "x" is not a symbol

エラーの種類

TypeError

エラーの原因

予期しない型がありました。これは undefined または null の値でしばしば発生します。

また、Object.create()Symbol.keyFor() のようなある種のメソッドは、特定の型を要求し、それを提供する必要があります。

無効な場合

js
// undefined と null の場合、substring メソッドは動作しません。
var foo = undefined;
foo.substring(1); // TypeError: foo is undefined

var foo = null;
foo.substring(1); // TypeError: foo is null

// ある種のメソッドでは、特定の型が求められることがあります。
var foo = {};
Symbol.keyFor(foo); // TypeError: foo is not a symbol

var foo = "bar";
Object.create(foo); // TypeError: "foo" is not an object or null

問題の修正

null ポインターを undefined 値に修正するには、次のように typeof 演算子を用いて行うことができます。

js
if (foo !== undefined) {
  // これで、 foo が定義されていることがわかるので、実行することができます。
}

if (typeof foo !== "undefined") {
  // 同じというのは良い考えですが、この実装を使わないでください。 - 本当の
  // undefined の値と未宣言の変数が混同されて問題が発生する可能性があります。
}

関連情報

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy