From 074785cea106179cb3305637055ab0a009ca74f2 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:52 -0500 Subject: initial commit --- .../errors/cant_access_property/index.html | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 files/pt-br/web/javascript/reference/errors/cant_access_property/index.html (limited to 'files/pt-br/web/javascript/reference/errors/cant_access_property') diff --git a/files/pt-br/web/javascript/reference/errors/cant_access_property/index.html b/files/pt-br/web/javascript/reference/errors/cant_access_property/index.html new file mode 100644 index 0000000000..7c5c9affa3 --- /dev/null +++ b/files/pt-br/web/javascript/reference/errors/cant_access_property/index.html @@ -0,0 +1,60 @@ +--- +title: 'TypeError: can''t access property "x" of "y"' +slug: Web/JavaScript/Reference/Errors/Cant_access_property +tags: + - Erros + - JavaScript + - TypeError +translation_of: Web/JavaScript/Reference/Errors/Cant_access_property +--- +
{{jsSidebar("Errors")}}
+ +

Mensagem

+ +
TypeError: Unable to get property {x} of undefined or null reference (Edge)
+TypeError: can't access property {x} of {y} (Firefox)
+TypeError: {y} is undefined, can't access property {x} of it (Firefox)
+TypeError: {y} is null, can't access property {x} of it (Firefox)
+
+Exemplos:
+TypeError: x is undefined, can't access property "prop" of it
+TypeError: x is null, can't access property "prop" of it
+TypeError: can't access property "prop" of undefined
+TypeError: can't access property "prop" of null
+
+ +

Tipo de Erro

+ +

{{jsxref("TypeError")}}.

+ +

O que deu errado?

+ +

O acesso a propriedade foi realizado com um valor {{jsxref("undefined")}} ou {{jsxref("null")}}.

+ +

Exemplos

+ +

Casos inválidos

+ +
// casos undefined e null, onde o metódo substring não irá funcionar
+
+var foo = undefined;
+foo.substring(1); // TypeError: x is undefined, can't access property "substring" of it
+
+var foo = null;
+foo.substring(1); // TypeError: x is null, can't access property "substring" of it
+
+ +

Corrigindo o problema

+ +

Para corrigir o problema de valores undefined ou null,  você pode usar o operador typeof, como no exemplo abaixo.

+ +
if (typeof foo !== 'undefined') {
+  // Agora que sabemos que foo está definida, podemos prosseguir
+}
+ +

Veja também

+ + -- cgit v1.2.3-54-g00ecf