在JavaScript/ECMAScript 7中,有没有一种方法可以获取一个类有多少个输入?

huangapple go评论64阅读模式
英文:

in javascript/emca script 7, is there a to get how many an inputs a class has?

问题

我目前正在进行一个项目,需要用户生成一些相对不错的本地数据并保存。这将是重复的数据,类似但具有唯一的字符串。

我目前正在使用的系统是,我有一个输入,大致如下:

function createCustomJob() {
    let dateFrom = prompt("Enter date from:");
    let dateTo = prompt("Enter date to:");
    let description = prompt("Enter job description:");
    let isCurrentPosition = confirm("Is it a current position?");
    let publicOrPrivate = prompt("Enter public or private:");
    let position = prompt("Enter job position:");
    let name = prompt("Enter job name:");

    let obj = new Obj(dateFrom, dateTo, description, isCurrentPosition, publicOrPrivate, position, name);

然而,我想要改为使用HTML构建输入,所以我想知道是否有一种方法可以获取构造函数接收的值的数量?是否有类似于Obj.length的东西可以工作?在创建第一个对象之前,我可以动态创建所需数量的HTML输入,还是我需要“硬编码”这些内容?

英文:

i'm currently working on a project which requires the user to generate a relative decent chunk of data to be saved locally. and this will be repeated data which is similar, but unique strings.

what i'm currently working with is a system where i have an input that is more or less:

function createCustomJob() {
    let dateFrom = prompt("Enter date from:");
    let dateTo = prompt("Enter date to:");
    let description = prompt("Enter job description:");
    let isCurrentPosition = confirm("Is it a current position?");
    let publicOrPrivate = prompt("Enter public or private:");
    let position = prompt("Enter job position:");
    let name = prompt("Enter job name:");

    let obj = new Obj(dateFrom, dateTo, description, isCurrentPosition, publicOrPrivate, position, name);

i however want to instead build the input using html, so i'm wondering is there some sorta way to get how many values a constructor is getting? something such as Obj.length that works? before the first object is created so i can dynamically create the proper amount of html inputs needed, or would this be something i instead need to "hard code"?

答案1

得分: 1

我强烈建议您使用HTML表单,并使用提交处理程序来获取用户/您输入的所有数据。

您当前的实现方式将来很难维护。

至于问题:
您可以简单地在构造函数中使用数组,然后可以直接访问输入变量的数量。

或者

您可以创建一个对象,使用对象键进行命名方法以更好地检索数据。

使用Object.keys(obj),Object.values(obj)甚至Object.entries(obj)来访问变量。

Mozilla一直都有很好的参考资料可供查阅:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries?retiredLocale=de

如果您真的想要做您的问题所述的事情,您可以使用扩展运算符,这里是一个使用示例:

class Test{
    constructor(...rest){
        console.log(rest)
    }
} 
英文:

I highly would suggest you use a html form and use the submit handler to get all data that the user/you enters.

The way you are currently implementing it is very hard to maintain in the future.

As of the question:
You can simply use an array in the constructor and then you have direct access to the amount of the input variables.

Or

You can create an object and use the object keys for a named approach to better retrieve the data.

Use Object.keys(obj), Object.values(obj) or even Object.entries(obj) to access the variables.

Mozilla has always good references to look into:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries?retiredLocale=de

If you really want to do what your question is about, you can use the spread operator, here an example to use:

class Test{
    constructor(...rest){
        console.log(rest)
    }
} 

huangapple
  • 本文由 发表于 2023年6月6日 13:01:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76411561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定