Javascript 调用将数字返回为fn(10)或fn*10的函数

Javascript 调用将数字返回为fn(10)或fn*10的函数,javascript,Javascript,我正在尝试编写一个函数,该函数以毫秒为单位返回可以以下两种形式调用的天数: days(7) // Expected output 604800000 days * 7 // Expected output 604800000 下面的代码起作用,但修改了函数原型: Function.prototype.valueOf = function() { return this(); } Function.prototype.toString = function() { retur

我正在尝试编写一个函数,该函数以毫秒为单位返回可以以下两种形式调用的天数:

days(7) // Expected output 604800000
days * 7 // Expected output 604800000 
下面的代码起作用,但修改了函数原型:


Function.prototype.valueOf = function() {
    return this();
}
Function.prototype.toString = function() {
    return this.valueOf().toString();
}

const d = 1000 * 60 * 60 * 24
const days = function (n) { return n && n * d || d}

console.log(days(7)); // 604800000
console.log(days * 7); // 604800000

console.log(days.toString(), `${days}`) // 86400000 86400000
console.log(days) // [λ: days]

尽管有许多方法,但我无法在不修改原型的情况下完成这项工作。这里有一个尝试供参考。我还尝试使用代理

const d = 1000 * 60 * 60 * 24

function Days() {
    return function (n) { return n && n * d || d}
} 

Days.prototype.valueOf = function() {
    return this();
}
Days.prototype.toString = function() {
    return this.valueOf().toString();
}

const days = new Days()

console.log(days(7)); // 604800000
console.log(days * 7); // NaN

console.log(days.toString(), `${days}`) // function (n) { var $_$c = $_$wf(1);  ...
console.log(days) // [λ] 



期待中的感谢

可以对返回参数值的函数进行闭包,并实现返回一天值的toString函数

持续天数=d=>{ 功能fn{ 返回n*d; } f、 toString=函数{return d;}; 返回f; }1000 * 60 * 60 * 24; console.logdays7//604800000
console.logdays*7;//604800000您可以对返回参数值的函数进行闭包,并实现返回一天值的toString函数

持续天数=d=>{ 功能fn{ 返回n*d; } f、 toString=函数{return d;}; 返回f; }1000 * 60 * 60 * 24; console.logdays7//604800000 console.logdays*7;//604800000 上课日{ constructordays=1{ 这个.天=天; } 托斯特林{ 返回日*本周天; } 静态toString{ 返回86400000; } } const sevenDays=新的第7天; console.logsevenDays;//{天:7} console.log`${sevenDays}`;//604800000 console.logDay;//1天默认值=86400000 console.logDay*2;//2天=17280000 console.log7*天;//7天=604800000 console.logDay+86400000+86400000;//3天=259200000 上课日{ constructordays=1{ 这个.天=天; } 托斯特林{ 返回日*本周天; } 静态toString{ 返回86400000; } } const sevenDays=新的第7天; console.logsevenDays;//{天:7} console.log`${sevenDays}`;//604800000 console.logDay;//1天默认值=86400000 console.logDay*2;//2天=17280000 console.log7*天;//7天=604800000
console.logDay+86400000+86400000;//3天=259200000多亏了@Nina Scholz,我实现了以下目标:

const makeUnit = u => function (d) {
    const f = n => n * d
    f.valueOf = () => d
    f.toString = () => f.valueOf().toString()
    return f;
}(u);

const seconds = makeUnit(1000)
const minutes = makeUnit(seconds * 60)
const hours = makeUnit(minutes * 60)
const days = makeUnit(hours * 24)
const weeks = makeUnit(days * 7)

console.log(seconds(1)) // 1000
console.log(minutes(1)) // 60000
console.log(hours(1))   // 3600000
console.log(days(1))    // 86400000
console.log(weeks(1))   // 604800000

console.log(seconds * 1) // 1000
console.log(minutes * 1) // 60000
console.log(hours * 1)  // 3600000
console.log(days * 1)   // 86400000
console.log(weeks * 1)  // 604800000

console.log(days.toString(), `${days}`) // 86400000 86400000

感谢@Nina Scholz,这就是我实现的:

const makeUnit = u => function (d) {
    const f = n => n * d
    f.valueOf = () => d
    f.toString = () => f.valueOf().toString()
    return f;
}(u);

const seconds = makeUnit(1000)
const minutes = makeUnit(seconds * 60)
const hours = makeUnit(minutes * 60)
const days = makeUnit(hours * 24)
const weeks = makeUnit(days * 7)

console.log(seconds(1)) // 1000
console.log(minutes(1)) // 60000
console.log(hours(1))   // 3600000
console.log(days(1))    // 86400000
console.log(weeks(1))   // 604800000

console.log(seconds * 1) // 1000
console.log(minutes * 1) // 60000
console.log(hours * 1)  // 3600000
console.log(days * 1)   // 86400000
console.log(weeks * 1)  // 604800000

console.log(days.toString(), `${days}`) // 86400000 86400000
感谢Nina,我注意到${days}返回86400000,即带有倒勾,因此我修改为f.valueOf=函数{return d;};f、 toString=函数{返回f.valueOf.toString;};感谢Nina,我注意到${days}返回86400000,即带有倒勾,因此我修改为f.valueOf=函数{return d;};f、 toString=函数{返回f.valueOf.toString;};