有没有办法有一个DTO。
所以在我的后端部分,我有一个非常明确的域,例如客户端。
class Client {
protected $firstName;
protected $lastName;
}
实际上,它是一个包含特定属性的类。我想在我的前端部分有类似的东西。我想确保函数的对象是Client
的实例,并且可以引用特定的客户端属性。
另一个问题 - 这是组织 AngularJS (1.5.9( 应用程序的合适方法吗?它会降低应用程序性能吗?
附言我想在我的前端部分得到这样的东西
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
}
谢谢!
Javascript是一种非类型语言。简单地说,你无法实现你想要的。
解决方法可能是在Client
类中添加一个返回Enum
的方法getType()
,并在 Angular 中检查该字段。
如果你想要一个JS的"类型化"版本,请检查TypeScript。
从 ES6 开始,您可以使用类并执行以下操作
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.
查看 MDN 文档以获取更多详细信息,MDN - 类,MDN - 实例