Creando objetos con typescript y limitando su alcance ['ESP','ENG']

En este artículo que será relativamente corto, enseñare como crear una clase con “Typescript” y como manipular su contenido, de manera sencilla para que cada parte quede explicada.
Algo muy común con este lenguaje de programación es la programación orientada a objetos, quiere decir que uno puede organizar su código basándose en objetos para una mejor estructura.

In this article, which will be relatively short, I will teach how to create a class with “Typescript” and how to manipulate its content, in a simple way so that each part is explained. Something very common with this programming language is object-oriented programming, which means that you can organize your code based on objects for a better structure.
En este ejemplo primero creo una clase, que a pesar de no definirla desde un principio como en “Java” implícitamente la clase es de carácter público, por lo cual se puede acceder desde cualquier lugar a dicha clase con el método reservado “new”. Cuando se hace esto es necesario guardar dentro de una variable esta invocación de la clase para luego acceder a cada uno de sus métodos. Algo que hay que saber es que cuando uno necesita llamar un método dentro de la misma clase se debe utilizar el método reservado “this”, que muchos de los programados sabemos que es un dolor de cabeza aprender cómo funciona al principio, pero luego se vuelve mecánico su uso.
In this example, I first create a class, which despite not defining it from the beginning as in "Java", implicitly the class is public, so said class can be accessed from anywhere with the reserved method "new". When this is done, it is necessary to save this invocation of the class within a variable and then access each of its methods. Something you have to know is that when you need to call a method within the same class you must use the reserved method “this”, which many of us programmers know is a headache to learn how it works at first, but then it becomes a headache. mechanical its use.

class2.jpg
En este ejemplo se ve que utilizo un constructor para declarar el valor de alguna variable dentro de la clase desde un inicio, de manera que al instanciar la clase debo agregarle un parámetro para evitar que la variable empiece con un valor indefinido, asi es como el constructor toma como parámetro un valor y lo inserta dentro de la variable. De esta manera me evito utilizar condicionales para verificar si el valor exista u tener que adicionar el valor antes de ejecutar algún método.

In this example you can see that I use a constructor to declare the value of some variable within the class from the beginning, so when instantiating the class I must add a parameter to prevent the variable from starting with an undefined value, this is how the The constructor takes a value as a parameter and inserts it into the variable. This way I avoid using conditionals to verify if the value exists or having to add the value before executing a method.

Por ultimo si uno desea que el valor de algo dentro del clase en la cual se escribe toda la lógica sea de carácter inmutable se debe utilizar el método “private” antes de cada parte de la clase que uno desea que solo se pueda alterar dentro de la misma clase estrictamente.

Finally, if one wants the value of something within the class in which all the logic is written to be immutable, the “private” method must be used before each part of the class that one wants can only be altered within Strictly the same class.


0
0
0.000
1 comments
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support. 
 

0
0
0.000