研究调查问卷:请告诉我们您使用 Blockly 的体验
开始调查问卷
生成代码
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
代码生成是将工作区中的代码块转换为可执行的代码字符串的过程。
代码生成非常重要,因为它让代码块能够实际执行操作,例如求解算术表达式、让角色在迷宫中移动或配置网店!
Blockly 不会直接“运行”代码块。而是生成代码字符串,然后执行这些代码。
代码生成器
如需生成代码,您可以使用代码生成器实例。
以下代码段展示了如何为工作区中的块生成 JavaScript 代码:
// 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):2025-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[[["\u003cp\u003eCode generation transforms visual blocks into executable code strings, enabling actions like calculations and character movements within applications.\u003c/p\u003e\n"],["\u003cp\u003eBlockly utilizes code generators to produce code in various languages like JavaScript, requiring separate generators for each target language.\u003c/p\u003e\n"],["\u003cp\u003eEvery block possesses a block-code generator that dictates the specific code it produces for a given language, customizable to your application's needs.\u003c/p\u003e\n"],["\u003cp\u003eWhile Blockly handles code generation, executing the generated code is application-specific and necessitates your own implementation based on the environment.\u003c/p\u003e\n"]]],["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"],null,["# Generate code\n\nCode generation is the process of turning the blocks on a workspace into a\nstring of code that can be executed.\n\nCode generation is extremely important, because it is what allows your blocks to\nactually *do* things, like evaluate arithmetic expressions, move a character\nthrough a maze, or configure an online shop!\n\nBlockly doesn't \"run\" blocks directly. Instead you generate code strings, and\nthen execute those.\n\nCode generators\n---------------\n\nTo generate code, you use a code generator instance.\n\nThis code snippet shows how to generate JavaScript code for the blocks in a\nworkspace: \n\n // javascriptGenerator is a code generator that makes JavaScript strings.\n import {javascriptGenerator} from 'blockly/javascript';\n\n const code = javascriptGenerator.workspaceToCode(myWorkspace);\n\nFor more information about the different code generators that Blockly provides\nand how to access them, see [Language code generators](/blockly/guides/create-custom-blocks/code-generation/overview#language_code_generators).\n\nBlock-code generators\n---------------------\n\nEach block has an associated block-code generator that defines what code it\ngenerates. A block-code generator has to be defined for each individual language\nyou want to generate.\n\nThis code snippets defines a JavaScript block-code generator for a \"move\nforward\" block: \n\n javascriptGenerator.forBlock['my_custom_block'] = function(block, generator) {\n const steps = block.getFieldValue('FIELD_NAME');\n // moveForward is a function you would have to define yourself and provide\n // within your execution context.\n return `moveForward(${steps});\\n`;\n }\n\nFor more information about how to define your block-code generators, see\n[Block-code generators](/blockly/guides/create-custom-blocks/code-generation/overview#block-code_generators).\n\nExecution\n---------\n\nAfter you've generated the code, you need to figure out how to execute it.\nDeciding how to execute it is very application-specific, and outside the scope\nof Blockly.\n\nFor more information about ways to execute code, see\n[Generate and run code](/blockly/guides/create-custom-blocks/code-generation/overview#generate_and_run_code)."]]