Question about methods and classes in Javascript

1

My question is: To call the client method and total it is not necessary () since they are methods, but why to print if I need parentheses? If it is because it is a normal and current function, why can not you put function in front?

class Factura{ 
  constructor(numero, cliente, importe, IVA){
    this._numero = numero;
    this._cliente = cliente;
    this._importe = importe;
    this._IVA = IVA;
  }

  get cliente(){ // metodo get
    return this._cliente;
  }

  get total(){ // metodo get
    return this._importe + this._IVA;
  }

  
  imprimir(){ 
    document.write('la factura numero ' + this._numero + ' es de ' + this._cliente)
  }
}

let factura1 = new Factura(512, 'Transportes Miguelito', 100, 21); 

console.log(factura1._cliente);
console.log(factura1.total);
factura1.imprimir();
    
asked by francisco dwq 13.01.2018 в 12:56
source

1 answer

2

It's just the other way around: for imprimir you use parentheses because it is a method, in this case without arguments, although you could have them.

For cliente and total you do not use parentheses because they are properties and you use the proper syntax to assign and retrieve values of any property:

var valor = instancia.cliente;

or to set the value:

instancia.cliente = valor;

In your case you can not set the value of these properties because they only have a "getter" method ( get ) but do not have a "setter" method ( set ) to set the value. If they had it, you could do it too:

class Factura{ 
  constructor(numero, cliente, importe, IVA){
    this._numero = numero;
    this._cliente = cliente;
    this._importe = importe;
    this._IVA = IVA;
  }

  get cliente(){ // metodo get
    return this._cliente;
  }

  get total(){ // metodo get
    return this._importe + this._IVA;
  }

  
  imprimir(){ 
    console.log('la factura numero ' + this._numero + ' es de ' + this._cliente)
  }
}

let factura1 = new Factura(512, 'Transportes Miguelito', 100, 21); 

console.log('Cliente factura1 ', factura1.cliente);
console.log('Total factura1 ', factura1.total);
// No puedo establecer valor a propiedad cliente
factura1.cliente = 'Asier Villanueva';
console.log('Nuevo cliente factura1 ', factura1.cliente);
// Metodo sin argumentos
factura1.imprimir();

class Factura2{ 
  constructor(numero, cliente, importe, IVA){
    this._numero = numero;
    this._cliente = cliente;
    this._importe = importe;
    this._IVA = IVA;
  }

  get cliente(){ // metodo get
    return this._cliente;
  }
  
  set cliente(value){
    this._cliente = value;
  }

  get total(){ // metodo get
    return this._importe + this._IVA;
  }

  
  imprimir(saludo){ 
    console.log(saludo + '. La factura numero ' + this._numero + ' es de ' + this._cliente)
  }
}

let factura2 = new Factura2(512, 'Transportes Miguelito', 100, 21); 

console.log('Cliente factura2 ', factura2.cliente);
console.log('Total factura2 ', factura2.total);
// Sí puedo establecer valor a propiedad cliente
factura2.cliente = 'Asier Villanueva';
console.log('Nuevo cliente factura2 ', factura2.cliente);
// Método con argumentos
factura2.imprimir('Hola');

Your problem I think is more nomenclature. The same is thrown at me for saying some inaccuracies but I will try to make it clear at a general level.

In javascript almost everything is defined with functions: the definition of a class is not more than a special type of function, the constructor another, a getter or setter of a property is defined by a function that establishes or returns the value of this one, ...

In fact, I have sometimes heard it defined as a function-based language. Definition that I personally think is quite accurate, although it could provoke a broad debate.

On the other hand, the values (which we assign to the variables) of javascript can be of one of the types called primitive (boolean, Number, String, ...) or objects.

The objects (we will leave the particular case of the Array separately for now) can have properties and methods. Properties are elements of a particular type and serve to maintain the value of a feature of the object (in the case of the invoice: customer, invoice number, supplier, amount, ...). The way to assign or retrieve the values of these properties / characteristics of the object would be:

 // Recuperar valor
 var miVariable = instanciaObjeto.nombrePropiedad;
 // Asignar valor
 instanciaObjeto.nombrePropiedad = nuevoValor;

The methods are different actions that can be executed on the object. These methods may or may not receive one or more arguments, and may or may not return a value. These methods can be used to modify the internal state of the object (in the invoice there could be a añadirLineaFactura method that adds data from a new line: factura1.añadirLineaFactura(datosNuevaLinea); ) or return a calculated value (for example a method calcularIVA : var importeIVA = factura1.calcularIVA(); ).

I hope I have cast a little light.

    
answered by 13.01.2018 в 13:07