Structs in Javascript

后端 未结 8 1415
花落未央
花落未央 2020-12-12 09:29

Previously, when I needed to store a number of related variables, I\'d create a class.

function Item(id, speaker, country) {
    this.id = id;
    this.speak         


        
8条回答
  •  感动是毒
    2020-12-12 09:46

    I think creating a class to simulate C-like structs, like you've been doing, is the best way.

    It's a great way to group related data and simplifies passing parameters to functions. I'd also argue that a JavaScript class is more like a C++ struct than a C++ class, considering the added effort needed to simulate real object oriented features.

    I've found that trying to make JavaScript more like another language gets complicated fast, but I fully support using JavaScript classes as functionless structs.

提交回复
热议问题