研究调查问卷:请告诉我们您使用 Blockly 的体验
开始调查问卷
代码生成
代码生成是将工作区中的块转换为
可以执行的代码字符串。
代码生成极其重要,因为代码生成的作用是
执行一些操作,例如对算术表达式求值、移动字符
或者配置网店!
Blockly 无法“运行”屏蔽规则您需要生成代码字符串,
然后执行这些命令
代码生成器
如需生成代码,请使用代码生成器实例。
此代码段展示了如何为
工作区:
// javascriptGenerator is a code generator that makes JavaScript strings.
import {javascriptGenerator} from 'blockly/javascript';
const code = javascriptGenerator.workspaceToCode(myWorkspace);
如需详细了解 Blockly 提供的各种代码生成器
以及如何访问它们,请参阅代码生成器概览。
块代码生成器
每个块都有一个关联的块码生成器,该生成器定义了它的
生成的内容。必须为每种语言分别定义一个块码生成器
生成特定文件。
此代码段定义了一个用于“移动”操作的 JavaScript 块码生成器。
转发”屏蔽:
javascriptGenerator.forBlock['my_custom_block'] = function(block, generator) {
const steps = block.getFieldValue('FIELD_NAME');
// moveForward is a function you would have to define yourself and provide
// within your execution context.
return `moveForward(${steps});\n`;
}
有关如何定义块码生成器的详细信息,请参见
块代码生成器。
执行
生成代码后,您需要确定如何执行代码。
执行方式因应用而异,不在范围内
是 Blockly 的
如需详细了解执行代码的方法,请参阅
代码执行。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-08-22。
[null,null,["最后更新时间 (UTC):2024-08-22。"],[[["Code generation transforms visual blocks into executable code strings, enabling actions like calculations and character movements within applications."],["Blockly utilizes code generators to produce code in various languages like JavaScript, requiring separate generators for each target language."],["Every block possesses a block-code generator that dictates the specific code it produces for a given language, customizable to your application's needs."],["While Blockly handles code generation, executing the generated code is application-specific and necessitates your own implementation based on the environment."]]],["Code generation converts blocks into executable code strings. Blockly utilizes code generator instances, like `javascriptGenerator`, to transform a workspace's blocks into code. Each block requires a block-code generator, defining its code output, demonstrated in the example with a \"move forward\" block. After code generation, the code must be executed, but this execution process is application-specific and not part of Blockly's core functionality.\n"]]