请将官方 ECMAScript 规范视为您的答案来源,而不是特定浏览器 vendor 发布的文档。 (我知道 Mozilla 使用“函数语句”扩展其 JavaScript 实现。)
因此,根据 ECMAScript 规范,其中定义的句法产生式,有效吗?
if (foo) {
function x() { return; }
}
更新: 我的问题也可以这样表述:Can the Statement production contain the FunctionDeclaration production ?
结论:答案是否定的。
最佳答案
我不同意说它有效的其他答案。
根据ECMA-262 5th Edition specification , Blocks 只能包含 Statements ( Section 12.1 ):
Block :
{ StatementList opt }
StatementList :
Statement
StatementList Statement
但是规范没有定义函数语句,而只定义了一个FunctionDeclaration 和一个FunctionExpression。规范进一步在 Section 12 中对此做了说明。 :
Several widely used implementations of ECMAScript are known to support the use of
FunctionDeclarationas aStatement. However there are significant and irreconcilable variations among the implementations in the semantics applied to suchFunctionDeclarations. Because of these irreconcilable difference, the use of aFunctionDeclarationas aStatementresults in code that is not reliably portable among implementations. It is recommended that ECMAScript implementations either disallow this usage ofFunctionDeclarationor issue a warning when such a usage is encountered. Future editions of ECMAScript may define alternative portable means for declaring functions in aStatementcontext.
要进一步阅读,您可能也有兴趣查看 comp.lang.javascript FAQ Section 4.2 :
4.2 What is a function statement?
The term function statement has been widely and wrongly used to describe a
FunctionDeclaration. This is misleading because in ECMAScript, aFunctionDeclarationis not aStatement; there are places in a program where aStatementis permitted but aFunctionDeclarationis not. To add to this confusion, some implementations, notably Mozillas', provide a syntax extension called function statement. This is allowed under section 16 of ECMA-262, Editions 3 and 5.Example of nonstandard function statement:
// Nonstandard syntax, found in GMail source code. DO NOT USE. try { // FunctionDeclaration not allowed in Block. function Fze(b,a){return b.unselectable=a} /*...*/ } catch(e) { _DumpException(e) }Code that uses function statement has three known interpretations. Some implementations process
Fzeas a Statement, in order. Others, including JScript, evaluateFzeupon entering the execution context that it appears in. Yet others, notably DMDScript and default configuration of BESEN, throw aSyntaxError.For consistent behavior across implementations, do not use function statement; use either
FunctionExpressionorFunctionDeclarationinstead.Example of FunctionExpression (valid):
var Fze; try { Fze = function(b,a){return b.unselectable=a}; /*...*/ } catch(e) { _DumpException(e) }Example of FunctionDeclaration (valid):
// Program code function aa(b,a){return b.unselectable=a}
关于javascript - 函数声明可以出现在 JavaScript 的语句中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4071292/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html
我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法