- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有以下内容:
const a = val => val;
const b = [a];
const results = b.map(fn => fn('x'));
我很想避免在 map 中创建额外的函数,这样我就可以得到这样的结果:(我知道这样我也无法传递参数)
const results = b.map(Function.call); // This is not working
当我这样做时,Chrome 控制台显示以下错误:
VM1075:3 Uncaught TypeError: undefined is not a function
我也试过使用:
const results = b.map(Function.prototype.call);
1) 为什么它不起作用/为什么我会收到此错误消息?
2)
我该如何解决?
我如何修复它总是传递相同的参数? (类似于 Function.call.bind(this, 'param')
最佳答案
.map(Function.call)
将不起作用,因为当方法作为回调传递时上下文会丢失。并且上下文应该是被调用者本身。所以为了让它按预期工作(没有绑定(bind) 'x'
)它应该是
.map(fn => Function.call.bind(fn)())
考虑到第二个 'x'
参数应该在 第一个 fn
参数之后绑定(bind)到映射器函数,所以无法获得映射回调单独使用一系列 call
和 bind
方法。
在 ES6 中通常的做法是
const xMapper = fn => fn('x');
...
const results = b.map(xMapper);
如果不担心可读性和可重用性,匿名 map
回调是完全没问题的。
关于javascript - 如何自动调用数组中的函数(ecma6),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44240056/
我刚刚访问了official github page TC39 并注意到一些名称以数字结尾,例如: TC39 ecma-262 ecma-402 这些数字从何而来?为什么不是 TC47、ecma-77
我正在编写一个基于 ECMA-262 的 JavaScript 解析器.我很想知道我需要改变多少才能做到这一点 ECMA-357兼容。 有语法差异吗? 最佳答案 有许多语法扩展。最重要的是 XML 文
在 Ecma-334(C# 语言规范第 4 版)中,附件 A. 语法,A.1.1 节行终止符: new-line:: Carriage return character (U+000D) Li
我正在尝试在我的 CQ 组件中运行一些服务器端 ECMAScript。 有没有办法在我的 /components/myComp/script.ecma 中包含/导入/需要其他文件(实际上是 Under
原因是,虽然我们可以在Javascript中使用伪经典继承,但实际上我们必须实现自己的extend或inherit。 关于原型(prototype)继承——我认为它确实有这样的特性,如果使用 foo.
我正在寻找 ECMAScript 替代方案来作为自定义应用程序逻辑的脚本语言。 ECMA 有一些我喜欢的地方,尤其是新规范(如 AS3)的语法。 不幸的是,AS3 引擎不是开源的,无法集成到项目中。我
我有一个服务在我的 CQ5 实例上运行,我想从工作流中调用它的方法。我的问题是,如何从 ECMA 脚本中获取服务的引用? 我已经试过了: var myService= sling.getService
在 ReactJS 中工作,我有许多具有相同导入的类: import MyClass from './some/path/foo.js'; import MyClass2 from './some/p
我正在阅读以下内容 presentation ,最后它提到了将在 ECMA6 脚本中引入的后缀条件。它提供了以下用例场景: console.log("42") if question is true;
有人知道如何在 ECMA Script(datapower) 中使用 xpath 表达式访问 XML 数据吗? IBM 信息中心没有关于如何访问 XML 数据的信息 如果您有任何用于访问 XML 数据
有谁知道吊装规范是什么时候出台的? Internet Explorer 版本 6 是否支持它? 我没有意识到提升的存在,因为最近我的所有 JavaScript 代码都被转译了。 当我向老板解释他的 J
我试图理解 JavaScript 中 === 运算符的确切算法。它被定义为类似 The comparison x === y, where x and y are values, produces t
首先 – 我们处于未涉足的领域,因此虽然它可以在最新的 Firefox 中运行,但 MDN 上的文档在撰写本文时尚未准备好。稍后我会修复MDN(也许还有很多地方需要修复),所以我会提供一个glossa
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
首先:使用 Actipro SyntaxEditor 中的 LL(*) 解析器。不过,这可以适用于任何 LL(k) 解析器。 我目前正在尝试为 Javascript 编写一个有效的 LL(k) 语法(
我在查找 JavaScript 的新函数时发现了 ECMAScript/ECMA 5。 因为我从未听说过它,所以我仔细研究了它,发现它以不同的名称形式出现,例如: JavaScript, JScrip
最近,我阅读了 ECMAScript 语言规范。我不打算阅读整个规范,只是挑选了一些部分。我遇到了很多问题,其中一个是这样的: ToLength 1.Let len be ToInteger(argu
我想知道为什么 ECMA 脚本不支持开箱即用的整数。我当然知道有某种整数(这个问题的答案中有一个很好的总结:Difference between floats and ints in Javascri
我在通过 JQuery 查询 SharePoint 列表 (2010) 时遇到此错误集合尚未初始化。尚未请求或请求尚未执行。可能需要明确请求。 我循环浏览 html 选择元素中的所有选项并查询 Sha
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
我是一名优秀的程序员,十分优秀!