如何检查JavaScript绑定函数 [英] How to inspect a JavaScript Bound Function

查看:54
本文介绍了如何检查JavaScript绑定函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以检查JavaScript绑定函数吗?

is there any way to inspect a JavaScript bound function?

我想从一个函数返回一个绑定函数,并在单元测试中声明该绑定函数的目标,boundThis和boundArgs.它们似乎在ECMAScript中定义为内部对象,无法在程序中访问.

I want to return a bound function from a function, and in unit testing, to assert the bound function's target, boundThis, and boundArgs. They seem to be defined as internal object in ECMAScript and cannot be accessed in program.

有什么办法可以访问它们?还是有些有类似要求的人已经写了一些模块补丁Function.prototype.bind()?

Is there any way to access them? or maybe some people with similar requirements have already written some module patching Function.prototype.bind()?

推荐答案

不,没有(除非您启动调试器).

No, there is not (unless you fire up a debugger).

无论如何,您都不应该测试绑定函数.您也可以使用闭包来实现完全相同的行为,也不会暴露闭包变量.没关系.您不应该测试实现,而是测试行为-因此,只需检查调用返回的函数是否确实以期望值调用目标即可.

You should not test for bound functions anyway. You could achieve the exact same behaviour with a closure as well, which would not expose the closed-over variables either. And it doesn't matter. You should not test the implementation, but the behaviour - so just check whether calling the returned function does invoke the target with the expected values.

这篇关于如何检查JavaScript绑定函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆