[英]How to create complex object structure in javascript?
I'm trying to create a structure of object using classes in javascript.我正在尝试使用 javascript 中的类创建对象结构。 I have a problem creating objects which involve nested structure.
我在创建涉及嵌套结构的对象时遇到问题。 For example ..
例如 ..
{
"id" : 1,
"name" : "mark",
"phoneNumber" : "123456789",
"dob" : "2010-08-20",
"address" : {
"latitude" : 123214.1231,
"longitude" : 1231243.12,
"houseNumber" : "1-2-3",
"landmark" : "square-garden",
"pinCode" : "134567876"
}
}
I think this way works, Instead of using class just use functions:我认为这种方式有效,而不是使用类只使用函数:
function AddressDto(latitude, longitude, houseNumber, landmark, pinCode) {
this.latitude = latitude || 0;
this.longitude = longitude || 0;
this.houseNumber = houseNumber || '';
this.landmark = landmark || '';
this.pinCode = pinCode || '';
}
function UserCreationDto(id, name,phoneNumber, dob, address) {
this.id = id || 0;
this.name = name || '';
this.phoneNumber = phoneNumber || '';
this.dob = dob || '';
this.address = address || new AddressDto();
}
const user = new UserCreationDto();
user.id = 1;
user.name = 'test';
user.phoneNumber = '1234567890';
user.dob = '12/12/12';
console.log(user.address.latitude);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.