[英]In native Javascript (or NodeJS) is it possible to chain object or function properties for a function call?
More of an exercise in 'what-if', I was wondering if the following was possible:更多关于“假设”的练习,我想知道以下是否可能:
output = convert(1200).from('mm').to('inches')
where 'from' and 'to' are functions (or properties) of 'convert' as opposed to the more standard:其中 'from' 和 'to' 是 'convert' 的函数(或属性),而不是更标准的:
output = convert(1200, 'mm', 'inches')
or:
output = convert(value = 1200, from = 'mm', to = 'inches')
addendum : I'm guessing the closest would be:附录:我猜最接近的是:
output = convert({ value: 1200, from: 'mm', to: 'inches' });
function convert({ value, from, to } = {}){
// ...do stuff here...
}
Yes, it's possible.是的,这是可能的。 Example:例子:
function convert(val) { const units = { mm: 1, cm: 10, dm: 100, m: 1000, in: 25.4, inches: 25.4, inch: 25.4, ft: 304.8, feet: 304.8, foot: 304.8, yd: 914.4, yard: 914.4, yards: 914.4 } return { from(unit1) { return { to(unit2) { return val * units[unit1] / units[unit2]; } }; } }; } const output = convert(1200).from('mm').to('inches'); console.log(output); console.log(convert(47.24409448818898).from('inches').to('mm')); console.log(convert(123).from('m').to('yards'));
convert
returns an object with a from
method. convert
使用from
方法返回 object。 from
returns an object with a to
method. from
返回一个带有to
方法的 object。 to
returns a number. to
一个数字。 The temporary values are stored in a closure.临时值存储在闭包中。
[Updated answer considering the comment from Teemu] [考虑到Teemu的评论更新答案]
Create an object constructor with the convert, from and to functions, and properties to store the values passed to each corresponding function when called.创建一个 object 构造函数,其中包含 convert、from 和 to 函数,以及在调用时存储传递给每个对应 function 的值的属性。 In both convert
and from
functions, return the instance via 'return this' and return the answer in the to
function.在convert
和from
函数中,通过“return this”返回实例并将答案返回to
function。
https://www.w3schools.com/JS/js_object_constructors.asp https://www.w3schools.com/JS/js_object_constructors.asp
function Converter() {
this.value = 0;
this.fromUnit = '';
this.toUnit = '';
this.convert = function(value) {
...
return this
}
this.from = function(unit) {... return this}
this.to = function(unit) {... return answer}
}
const converter = new Converter()
converter.convert(...).from(...).to(...)
You can also follow the Class syntax on ES6 JavaScript.您还可以遵循 ES6 JavaScript 上的 Class 语法。 https://www.w3schools.com/Js/js_classes.asp https://www.w3schools.com/Js/js_classes.asp
Pardon the formating as I'm answering on my phone.请原谅我在手机上接电话时的格式。
Treating this as a syntax rather than a design question, here is an ugly but working solution.将其视为语法而不是设计问题,这是一个丑陋但可行的解决方案。
However, I would like to encourage you to refactor it or reconsider whether this functional API is necessary.但是,我想鼓励您重构它或重新考虑这个功能 API 是否必要。
const convert = (num) => ({ from: (inputUnit) => { switch (inputUnit) { case "mm": return { to: (outputUnit) => { switch (outputUnit) { case "inches": return num / 25.4; } }, }; } }, }); console.log( convert(1200).from("mm").to("inches") )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.