Structs in Javascript

后端 未结 8 1386
花落未央
花落未央 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:45

    I made a small library to define struct if you work with ES6 compatibility.

    It is a JKT parser you may checkout the project repository here JKT Parser

    For an example you may create your struct like this

    const Person = jkt`
        name: String
        age: Number
    `
    
    const someVar = Person({ name: "Aditya", age: "26" })
    
    someVar.name // print "Aditya"
    someVar.age // print 26 (integer)
    
    someVar.toJSON() // produce json object with defined schema 
    

提交回复
热议问题