javascript - Javascript面试题(mul函数)
问题描述
知道这是如何工作的吗?
function mul(x) {
return function(y) {
return [x*y, function(z) {
return x*y + z;
}];
}
}
console.log(mul(2)(3)[0]);
console.log(mul(2)(3)[1](4));
我不确定在 mul 函数中给出索引是如何工作的
解决方案
罗比是正确的。扩展他的解释......第一个console.log;
console.log(mul(2)(3)[0]);
正在返回调用前两个函数后返回的数组的索引 0... x*y,即 2*3 = 6。第二个 console.log;
console.log(mul(2)(3)[1](4));
在该点返回索引 1,它返回将 z 作为参数的函数......一旦将 z 传递给函数,它返回 x*y + z,即 2*3 + 4 = 10。
推荐阅读
- aem - 如何在 AEM 6.4 中的 MSM 中的特定目标页面上使用 Rollout Config Action Class 放弃推出
- apache - Apache 作为反向代理和负载均衡器需要使用粘性会话
- vb.net - 在所需位置的 ImageBox 上绘图
- c - 超过4个参数的“snprintf”有什么用?
- java - 矩阵上的 Dijkstra 算法
- typescript - 如何使用打字稿模块扩充修补类构造函数?
- docker - Nginx 代理管理器(NGINX 反向代理)可以连接到 Cloudflare Argo 隧道吗?
- mongodb - Mongoose如何将元素推送到数组数组的模式
- c# - c# 将字符串解析为日期。'yyyyMMddHHmmss.ffffff+zzz'
- java - 值未存储在不同方法之间的变量中(初学者java项目)