Think of the class, as a structure similar to a JSON, JSON is a simple structure, similar to XML, it is worth noting that the class is a template/template, and the JSON object is a custom model with values or status:
Your class:
class Pessoa {
}
When you build a JSON, you will write something similar to this:
var pessoa = {propriedade: "valor"}
When you assign a method to a property in your class, you will do something similar to this:
class Pessoa {
propriedade: function() {
}
}
See the example below, imagine you tried to do the same with a JSON structure.
Have you ever seen someone make statements inside a JSON like this? You were able to identify the problem below?
//ISSO É IMPOSSÍVEL!!!
var pessoa = {
var endereco;
}
So... without declaring with "var" it will work, because it becomes a property, which is part of the scope of the keys of your class. And since in this case you have not assigned values or methods to this property, it becomes an undefined property:
pessoa.endereco //retornará undefined
Worth a read here to better understand
What will be the value of
endereco
if I instantiatePessoa
and not call the methodsetEndereco
?– Woss
I believe Undefined because every variable not initialized in JS has this value as default
– Gabriel