【问题标题】:How to create complex object structure in javascript?如何在javascript中创建复杂的对象结构?
【发布时间】:2020-09-17 17:28:49
【问题描述】:

我正在尝试使用 javascript 中的类创建对象结构。我在创建涉及嵌套结构的对象时遇到问题。例如..

{
    "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"
    }
}

address 属性具有单独的内部结构。如何为上面的 JSON 创建一个类? 我对 JS 有点陌生。请帮我。以下是我尝试过的事情。

我有一个类 UserCreationDto

class UserCreationDto {

    id;
    name;
    phoneNumber;
    dob;
    address;

    constructor(id, name, phoneNumber, dob, address) {
        this.id = id;
        this.name = name;
        this.phoneNumber = phoneNumber;
        this.dob = dob;
        this.address = address;
    }
}

还有一个AddressDto类

class AddressDto {

    latitude;
    longitude;
    houseNumber;
    landmark;
    pinCode;

    constructor(latitude, longitude, houseNumber, landmark, pinCode) {
        this.latitude = latitude;
        this.longitude = longitude;
        this.houseNumber = houseNumber;
        this.landmark = landmark;
        this.pinCode = pinCode;
    }
}

我希望UserCreationDtoaddress 属性属于AddressDto。我怎样才能做到这一点?以上类UserCreationDto可以使用。 但是,当我这样做时没有智能感知

let userCreationDto = new UserCreationDto();
userCreationDto.address.{i-get-random-properties-here}

我试过这样做

UserCreationDto.prototype.address=new AddressDto()

通过上述方法,我获得了 IntelliSense,但使用原型,UserCreationDto 的所有实例共享值。

如果我只是继续上面的课程UserCreationDto,那么我不会得到任何 IntelliSense。

IDE 允许将任何内容推送到 address 属性。

如何创建如上所述的嵌套结构?什么是类结构?

【问题讨论】:

  • new UserCreationDto (1, 2, 3, 4, new AddressDto(1, 2, 3, 4, 5))
  • @VLAZ 如何为此编写类?创建一个对象实例很好。帮助我创建课程。
  • 您已经创建了类。如果你正确地实例化它们,那么你也会得到正确的数据。 jsbin.com/vijeqoroqi/edit?js,console
  • @VLAZ 你说上面的课程很好。所以,当我从一些 http-post-request 得到一个像上面这样的对象时,首先,我应该创建 AddressDto ,填充它。然后创建 UserCreationDto 并将地址属性分配给已创建的 AddressDto 实例。这会很好。有没有办法获得智能感知?我的意思是,我们得到 id、name、phoneNumber、dob、address 的智能感知,因为它们是类的直接属性。如何从 userCreationDto Instance 获取地址属性的智能感知?
  • @protonsphere 你用的是什么IDE?您需要告诉智能感知 address 属性的类型为 AddressDto

标签: javascript node.js express ecmascript-6


【解决方案1】:

我认为这种方式可行,而不是使用类,只使用函数:

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);

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    • 2018-12-12
    • 1970-01-01
    相关资源
    最近更新 更多