使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
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):2025-07-25。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-25。"],[],["The `provideFunction_()` method defines developer-created helper functions for generated code. It takes a `desiredName` and `code` (string or string array). The first call with a `desiredName` saves the `code` and generates a function name. Subsequent calls with the same `desiredName` are ignored. The function name is returned, and may differ from `desiredName` if a name conflict is detected. The code is output when `CodeGenerator.finish()` is called. The method is used to prevent name collisions with user-defined items.\n"]]