How do I make a variable of the same type of class in Javascript?

1

I want to make a simple list linked with javascript and for that, I need to make a variable of the same type of the class, in Java I did it like this:

class Nodo{

    public char datoNodo; 

    public Nodo sgte;


 public Nodo(char dato){
  this.dato=dato;
 } 


}

Since javascript can not specify the type of variable, it is only specified with 'var'.

    
asked by Jean Luis Soto Robles 09.08.2018 в 23:07
source

2 answers

1

You can write something like this:

class Nodo{

        constructor(Nodo, dato){
            this.Nodo= Nodo;
            this.dato=dato;
        }

    }

let my_nodo1 = new Nodo();
let my_nodo2 = new Nodo(my_nodo1,"Hola, usuario");
console.log(my_nodo2.dato);

It's equivalent to what you do with Java.

    
answered by 09.08.2018 в 23:22
1

Hello I hope to be of help.

javascript is not highly typed, so when creating the variable you do not need to define it with its type, because with var it should be enough,

but I recommend you use let is the modern way to define variables in javascipt

greetings

    
answered by 09.08.2018 в 23:13