哈希与特定参数
我正在编写一个小型 JavaScript 库,以使表单创建/验证更容易。 这主要是为了更好地使用 javascript 的借口。 我正在讨论如何接受用户输入来创建表单元素。
我正在考虑两种方法。 一种是接受一些预先确定的参数,例如 id、label、value。
另一种是接受单个对象,该对象将用于将属性直接写入表单元素。 因此,例如,如果我想传递 id label 和 value,我只需传递 {id : 'my_id', label : 'my_label', value : 'my_value}
第三个选项是接受 id, label, options ,其中options 是一个执行我上面描述的操作的对象。
现在传递一个对象似乎是最灵活的,但我想知道这是否有什么严重的缺点? 我希望大多数 javascript 用户都能轻松使用它。 关于最好的方法有什么想法吗?
I am in the process of writing a small javascript library to make form creation/validation easier. This is mostly an excuse to get better with javascript. I am debating about how to take in user input for form element creation.
I am looking at two approaches. One is to take in a few pre-determined parameters such as id, label, value
The other is to take in a single object which will be used to write properties straight into the form element. So for example if I wanted to pass in id label and value, I would simply pass in {id : 'my_id', label : 'my_label', value : 'my_value}
The third option is to take in id, label, options where options is an object which does what I have described above.
Right now the passing an object in seems the most flexible, but I wonder if there are any serious downsides to this? I would like this to be easy to use for most javascript users.
Any thoughts on the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
处理这个问题的通常方法是使用选项对象,就像您的第二选择一样。 大多数需要选项的插件(和 jQuery 方法)都使用该模式。 据我所知,这没有任何缺点,并且由于它是处理选项的一种非常常见的方式,我不认为它对其他人来说很难使用。
如果您有绝对必须传入的内容(例如),选项 3 也是可以接受的。 ID。 正是在这种情况下,选项对象最常作为函数的最终参数。
希望这可以帮助
The usual way of handling this is with an options object as in your second choice. Most plugins (and jQuery methods) that require options use that pattern. As far as I'm aware there are no downsides to this and as its a very common way of handling options, I don not see it being hard to use for others.
Option 3, is also acceptable if you have something that absolutely must be passed in eg. id. Just in that case the options object is most commonly found as the final parameter of the function.
Hope this helps
在我研究过的 JS 代码中,使用对象似乎是“标准”方法。
使用特定参数列表会增加排序要求,当您有多个参数时,很快就会失控。
如果您有“依赖”参数,例如,仅针对另一个参数的某些值出现的参数,那么在使用简单的参数列表时,这尤其难看。
但是,在迭代对象的属性时要小心。 很容易获得非预期的继承元素。 Douglas Crockford 建议使用 hasOwnProperty() 来检查这种情况。
Using objects seems to be the "standard" way to do that in the JS code that I've studied.
Using a list of specific parameters, which adds an ordering requirement, quickly gets out of hand when you have more than a few parameters.
If you have a "dependent" parameters, e.g., ones that only appear for certain values of another parameter, then that's especially ugly when using a simple parameter list.
Be careful when iterating over the properties of an object, however. It's easy to get inherited elements that aren't intended. Douglas Crockford recommends using hasOwnProperty() to check for this case.
您可以通过一些技巧来支持两者。 您可以检查所有传递给函数的参数:
您可以然后使用如下条件:
这基本上是说,如果第一个参数是一个对象,则提取 id,否则直接使用该参数。
我更喜欢对象方式,它适合外部 JSON 数据源使用。
You can support both, with a bit of hackery. You can inspect the all the arguments passed to the function:
You can then use a condition like :
This basically says that if the first argument is an object, extract the id, else use the argument directly.
I prefer the object way, it lends itself to be used by external JSON data sources.