1

Is there a way to have a DTO.

So on my back-end part I have a pretty clear domains, e.g. Client.

class Client {
    protected $firstName;
    protected $lastName;
}

In fact it is a class, that contain specific properties. I want to have something similar on my front-end part. I want to be sure that the object came to a function is an instance of Client and I can refer to specific Client properties.

Another question - is it an appropriate approach to organize AngularJS (1.5.9) application?. Would it decrease application performance?

P.S. I want to get something like this on my front-end part

function someFunc(client) {
    if (!(client instanceof Client)) {
        // handle error
    }
// here I can refer to client.firstName or client.lastName and not get undefined, as long as it is a required Client properties
}

Thank you!

Majesty
  • 2,097
  • 5
  • 24
  • 55

2 Answers2

1

Javascript is an untyped language. Put that simply, you cannot achieve what you want.

A workaround could be to add a method getType() in your Client class with an Enum returned and check that field in Angular.

If you want instead a "typed" version of JS check TypeScript.

Narmer
  • 1,414
  • 7
  • 16
1

as of ES6 you can use classes and do something like this

var Animal = {
  speak() {
    console.log(this.name + ' makes a noise.');
  }
};

class Dog {
  constructor(name) {
    this.name = name;
  }
}

// If you do not do this you will get a TypeError when you invoke speak
Object.setPrototypeOf(Dog.prototype, Animal);

var d = new Dog('Mitzie');
d.speak(); // Mitzie makes a noise.

check out MDN docs for further details, MDN - Classes, MDN - instanceof

Daniel Netzer
  • 2,151
  • 14
  • 22