研究调查问卷:请告诉我们您使用 Blockly 的体验
开始调查问卷
blockly >代码生成器 >CodeGeneratorprovideFunction_
CodeGenerator.provideFunction_() 方法
定义要包含在生成的代码中的开发者定义的函数(而非用户定义的过程)。用于创建私有辅助函数。首次使用给定的 requiredName 调用此方法时,系统会保存代码并生成实际名称。具有相同 requiredName 的后续调用不会产生任何影响,但具有相同的返回值。
调用方应确保不同的辅助函数没有使用相同的 requiredName(例如,使用“colourRandom”和“listRandom”,而非“random”)。不会与保留字、用户定义的变量或过程名称发生冲突。
调用 CodeGenerator.finish() 时,代码会输出。
Signature:
provideFunction_(desiredName: string, code: string[] | string): string;
参数
参数 |
类型 |
说明 |
desiredName |
字符串 |
所需的函数名称(例如 mathIsPrime)。 |
代码 |
字符串 [] |字符串 |
语句列表或一个多行代码字符串。使用“。(将会被替换)。 |
返回:
字符串
新函数的实际名称。如果用户已占用该 ID,则此 ID 可能与 requiredName 不同。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-08-21。
[null,null,["最后更新时间 (UTC):2024-08-21。"],[[["`CodeGenerator.provideFunction_()` defines developer-defined helper functions for inclusion in the generated code, assigning a unique name to avoid conflicts."],["Calling this method multiple times with the same `desiredName` has no effect but returns the previously assigned unique name."],["Generated helper function code is output when `CodeGenerator.finish()` is called."],["The method takes the desired function name and code as input, returning the actual assigned function name."],["Ensure distinct `desiredName` values for different helper functions to prevent unintended behavior."]]],[]]