Could you explain to me the construction part please, I don’t understand

Asked

Viewed 66 times

0

class Negociacao {

    **constructor(_data, _quantidade, _valor) {
        Object.assign(this, {
            _quantidade, 
            _valor
        });
        this._data = new Date(_data.getTime())
        Object.freeze(this);
    }**

    getVolume() {
        return this._quantidade * this._valor;
    }

    getData() {
        return new Date(this._data.getTime());
    }

    getQuantidade() {
        return this._quantidade;
    }

    getValor() {
        return this._valor;
    }

}
  • where you that code?

1 answer

0

analyzing the builder below, what happens:

    constructor(_data, _quantidade, _valor) {
        Object.assign(this, {
            _quantidade, 
            _valor
        });
        this._data = new Date(_data.getTime())
        Object.freeze(this);
    }
  1. 3 values are passed to this constructor, _data, _quantity and _value.
  2. The Object.assign() method, in this case the Trading class, values _quantity and _value (which in the class structure, are attributes of the object).
  3. Assigns the class the value of _data.
  4. The Object.Freeze() method freezes the object, i.e., no further modification of attributes outside the constructor is allowed.

So, as a conclusion, this constructor assigns initial values to the Trading class and freezes it, so that new values cannot be entered after class construction.

References:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze

Browser other questions tagged

You are not signed in. Login or sign up in order to post.