懒惰地评估javascript参数吗? [英] Are javascript arguments lazily evaluated?

查看:84
本文介绍了懒惰地评估javascript参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了一个依赖项注入模块.它使用我发现的带有默认参数的hack. function (x = SomeDependency) {}. SomeDependency尚未定义,但我可以解析其toString(与类构造函数,箭头函数和简洁的对象方法相同).并不是要在浏览器中支持它,而只能在Node中支持它.

I have made a dependency injection module. It uses a hack I discovered with default parameters. function (x = SomeDependency) {}. SomeDependency is not defined, but I can parse its toString (same for class constructors, arrow functions and terse object methods). It's not meant to be supported in the browser, only in Node.

我的问题:我找不到关于是否对参数进行延迟计算的文档,是否存在有关此规范/文档的文档?还是这仅仅是不确定的行为?

My question: I could not find any documentation on whether arguments are lazily evaluated, does any specification/documentation on this exist? Or is this simply undefined behaviour?

更新:

我正在做的是使用未定义的默认参数(如上例所示),然后解析函数/类的toString找出它们的含义,然后调用函数或使用注入的参数更新类./p>

What I am doing is using undefined default parameters (as in the example above), and parsing the toString of the function/class to find out what they are, then calling the function or newing up the class with injected arguments.

推荐答案

参数是否被懒惰地评估?

Are arguments lazily evaluated?

否,我们急切地对JavaScript中的所有内容进行评估(如果您对逻辑操作数进行短路评估,请谅解).

No, everything in JavaScript is eagerly evaluated (if you excuse short-circuit evaluation of logical operands).

具体来说,使用参数时不评估默认的参数初始化程序,而在调用函数时急切地评估它们.但是,它们是有条件地求值的-每当参数为undefined时,就非常类似于if子句中的语句.

Specifically, the default initialisers of parameters are not evaluated when the parameter is used, they are eagerly evaluated when the function is called. They are however evaluated conditionally - whenever the argument is undefined, pretty much like the statement in a if clause would be.

是否存在与此相关的任何规范/文档?

Does any specification/documentation on this exist?

是的,在ECMAScript标准中指定了JavaScript评估顺序(当前版本: http ://www.ecma-international.org/ecma-262/7.0/). 几乎没有未定义的行为.

Yes, JavaScript evaluation order is specified in the ECMAScript standard (current revision: http://www.ecma-international.org/ecma-262/7.0/). There is hardly any undefined behaviour.

这篇关于懒惰地评估javascript参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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