简体   繁体   中英

Rewriting Python Classes to JavaScript

I have some python classes written and being constructed like this:

class Customer:
def __init__(self, name, full_name, gender, age, instruments, paid):
    self.name = name
    self.full_name = full_name
    self.gender= gender
    self.age= age
    self.instruments= instruments
    self.paid= paid

list = []

list.append( Customer('Customer1', 'First Customer', 'Male', 26, ["Guitar", "Bass"], [100, 200]))
list.append( Customer('Customer2', 'Second Customer', 'Female', 24, ["Drums", "Bass"], [150, 230]))

I'm trying to rewrite my functions from python to JavaScript, and having some problems/doubts about this data structure.

The instruments and paid values(only example here) can be text values or numbers. When I access it and select "Customer 1" I should be able to choose the instrument to print the paid price

How do I write the same structure in JavaScript?

All this is that I am trying to change this personal python file to a customer local.html page.

Maybe JavaScript is not the best here and, since i am not proficient on that, I can spent some time in other language to handle it.

How would I write it in.js or, what would be the better option to go with local.html

If you glance at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

you could end up with

 class Customer { constructor(name, full_name, gender, age, instruments, paid) { this.name = name this.full_name = full_name this.gender = gender this.age = age this.instruments = instruments this.paid = paid } } list = [] list.push(new Customer('Customer1', 'First Customer', 'Male', 26, ["Guitar", "Bass"], [100, 200])) list.push(new Customer('Customer2', 'Second Customer', 'Female', 24, ["Drums", "Bass"], [150, 230])) console.log(list)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM