I'm wondering whats the best approach or if there is any consequence when referring to outside of the class constants in the same file. 

The problem arouse when I was declaring a simple class that would throw an error when receiving an invalid parameter:

```js
export class Digit {
  private _value: number = null;

  constructor(value: number) {
    this._value = value;
  }

  set value(value: number) {
    if (isNaN(value)) { throw new Error('Received NaN as digit.'); }

    this._value = value;
  }

  get value() {
    return this._value;
  }

}

```

That *value* setter would never trigger in the constructor as the class needs to be instantiated to do so.

Because I want to keep things simple, I just want this example class to hold a valid digit or to not exist at all, so I don't want to instantiate it with null on its *value*. This led me to the following code:

```js
export class Digit {
  private _value: number = null;

  constructor(value: number) {
    if (isNaN(value)) { throw new Error('Received NaN as digit.'); }

    this._value = value;
  }

  set value(value: number) {
    if (isNaN(value)) { throw new Error('Received NaN as digit.'); }

    this._value = value;
  }

  get value() {
    return this._value;
  }

}

```

Which works fine but now I'm repeating myself! Imagine doing a validation like that for 10 or more fields.

So I thought about two solutions:

1- Refactor into a validation function inside of class

```js
export class Digit {
  private _value: number = null;

  private readonly validateValue = function (value: number): number {
    if (isNaN(value)) { throw new Error('Received NaN as digit.'); }

    return value;
  };

  constructor(value: number) {
    this._value = this.validateValue(value);
  }

  set value(value: number) {
    this._value = this.validateValue(value);
  }

  get value() {
    return this._value;
  }

}

```

I like this approach because all the business logic is contained inside of the class. But its still reachable outside of this if you choose not to respect the private scope.
The thing i don't like about it is that the class gets cluttered easily as more and more fields and validations are added, then it gets distracting to the eye, as you may be wanting to fix some behavior knowing that your instance values are correct.

2- Refactor into a validation function outside of class but in the same file

```
const validateValue = function (value: number): number {
  if (isNaN(value)) { throw new Error('Received NaN as digit.'); }

  return value;
};

export class Digit {
  private _value: number = null;

  constructor(value: number) {
    this._value = validateValue(value);
  }

  set value(value: number) {
    this._value = validateValue(value);
  }

  get value() {
    return this._value;
  }

}

```

What I like about this approach is that I can go straight to my class code with the precondition that values are correct and validated and I'm not being distracted by them inside of my class. Plus I can't choose to omit a private scope and access its validators.

Also if validations get really extensive I can choose to create a helper for the class and refactor the validations outside of the file (don't know if that's a bad practice).

On the other hand I don't know about the consequences of using these outside of class declarations.

So what would be the best approach for this problem? Are both solutions valid? Is there a better approach?